Python Readline

Contents

Python – Read File Line by Line

Python中的read,readline,readlines区别与用法 python中读取文件常用的三种方法:read,readline,readlines.今天看项目是又忘记他们的区别了。 以前看书的时候觉得这东西很简单,一眼扫过,待到用时却也只知道有这么几个方法,不懂得它的原理与用法。. Python – Read File Line by Line There are many ways to read a text file line by line in Python. You can read the lines to a list, or just access them one by one in a loop by iterating over the lines provided by some kind of iterator or calling a function on the file object. Python readline function is the file handling method of python. This method is used to read a single line from the file and return it as a string. By default, it will return the complete line, but we can also define the size of the bytes that can be returned. If the file has no data. Python file method readline reads one entire line from the file. A trailing newline character is kept in the string. If the size argument is present and non-negative, it is a maximum byte count including the trailing newline and an incomplete line may be returned. An empty string is returned only when EOF is encountered immediately.

There are many ways to read a text file line by line in Python. You can read the lines to a list, or just access them one by one in a loop by iterating over the lines provided by some kind of iterator or calling a function on the file object.

In this tutorial, we will learn how to read a file line by line using readline() function, readlines() function, or file object, with the help of example programs.

Example 1: Read Text File Line by Line – readline()

In this example, we will use readline() function on the file stream to get next line in a loop.

Steps to use file.readline() function

Following are the steps to read file line by line using readline() function.

  1. Read file in text mode. It returns a stream to the file.
  2. Create an Infinite While Loop.
    1. During each iteration of the loop, read the next line from the file using readline().
    2. If the line is not empty, you have the next line. You can check this using if-not. Else, there are no more lines in the file, and we break the loop.
  3. By the time we break out of the loop, we have read all the lines of file one by one during the iterations.
  4. Since we are done with the file, we will close it.

Python Program

Output

Example 2: Read Lines as List – readlines()

readlines() function returns all the lines in the file as a list of strings. We can traverse through the list, and access each line of the file.

Python

In the following program, we shall read a text file, and then get the list of all lines in the text file using readlines() function. After that, we use For Loop to traverse these list of strings.

Python Program

Output

Example 3: Read File Line by Line using File Object

In our first example, we have read each line of file using an infinite while loop and readline() function. But, you can use For Loop statement on the file object itself to get the next line in the file in each iteration, until the end of file.

Following is the program, demonstrating how we use for-in statement to iterate over lines in the file.

Python Program

Python Readline Loop

Output

Summary

Python Readline

In this tutorial of Python Examples, we learned how to read a text file line by line, with the help of well detailed python example programs.

  • Python Basic Tutorial
  • Python Advanced Tutorial
Syntax
  • Python Useful Resources

Python Readline Without Newline

  • Selected Reading

Description

Python file method readline()reads one entire line from the file. A trailing newline character is kept in the string. If the size argument is present and non-negative, it is a maximum byte count including the trailing newline and an incomplete line may be returned.

An empty string is returned only when EOF is encountered immediately.

Syntax

Following is the syntax for readline() method −

Parameters

  • size − This is the number of bytes to be read from the file.

Return Value

Read Python

This method returns the line read from the file.

Python Readline Example

Example

Python Readline Until Eof

The following example shows the usage of readline() method.

Python Readline Gbk

When we run above program, it produces following result −