What is Python file readline() method?

A few main points about the readline() method:

  • The readline() method reads single line from the specified file.
  • If used in text mode then readline() returns string and returns byte object in binary mode.
  • A “\n”, the trailing newline character is left at the end of the string.
  • You may provide size argument in the readline() method, the optional numeric argument.
  • If you do not provide the size argument or a negative value is given then the entire content of the file is read and returned.
  • If the size argument is given then it is the maximum byte count. In that case, the newline character is also included and readline() method may return an incomplete line.

How to use the readline() method?

The general syntax for using the readline() method is:

FO.readline( size );

Where FO is the file object.

An example of readline() with text file without size

In the first example, I am using readline() method to return the content of a text file without specifying the size argument. So, the complete content should be returned and I will execute readline() only once to display the content.

For the clarity, the text file contains following demo content for all our examples below:

Python

Open

Read

Write

Close

The text file name is python-demo.txt and this is stored at the same location where our code file is placed.

The code:

# readline() example



objFile = open("python-demo.txt", "r")



print (objFile.readline());



objFile.close()

The output:

Python readline simple

You can see only the first line is displayed along with the “\n” character i.e. a line.

The example of specifying the size argument

In the following example, I provide the value 2 for the size argument in the readline() method. See what it returns:

# readline() example with size



objFile_size = open("python-demo.txt", "r")



print (objFile_size.readline(2));



objFile_size.close()

The output:

Python readline size

The readline() method only returned first two characters while the newline is also not included. If we specify the size 7 in the above example then it should return a newline as shown below:

print (objFile_size.readline(7));

The output:

Python readline

Reading next lines in text file example

The example below displays the first four lines from the demo text file including “\n” with each line. For that, the readline() method is used four times that will read the file line by line as follows:

The code:

# readline() example with size



objFile_multiple = open("python-demo.txt", "r")



print (objFile_multiple.readline());

print (objFile_multiple.readline());

print (objFile_multiple.readline());

print (objFile_multiple.readline());



objFile_multiple.close()

Python readline multiple

Using a for loop to display content

The memory efficient and fast way of reading the complete content from the file line by line is using a loop and iterate through multiple lines.

The example below uses a for loop with the file object (same file as in the above examples) and displays the complete content of the file. Have a look:

# readline() example with loop



objFile_loop = open("python-demo.txt", "r")



for line in objFile_loop:

    print(line)





objFile_loop.close()

Python readline loop

Just like the above examples, you may notice a line break is added.

You may also omit the newline character from displaying as shown in the example below (with for loop):

objFile_loop_omit = open("python-demo.txt", "r")



for line in objFile_loop_omit:

    print(line, end='')



objFile_loop_omit.close()

The result:

Python readline newline

You can see no gap between the lines in the above graphic.

What if size is given as negative?

As mentioned earlier, you may provide the value for size argument as negative or do not provide at all then readline() method returns complete file content.

You have seen an example in the above section with the positive value of 2 and it returned only two characters, now have a look at what happens when I used -2 value:

# readline() example with a negative number



objFileSizeNeg = open("python-demo.txt", "r")



print (objFileSizeNeg.readline(-2));



objFileSizeNeg.close()

The result:

readline negative

The first complete line is displayed including a newline character. That means, the complete content is returned by readline() method, although, we provided -2 value.

Using list to display file content (not line by line)

You may also use list(file_object) for reading the file’s content completely. The example below shows using the list and there you may also see “\n” is added with each element in the list:

objFileSizeNeg = open("python-demo.txt", "r")



print (list(objFileSizeNeg));

objFileSizeNeg.close()

The output of the above code:

[‘Python\n’, ‘Open\n’, ‘Read\n’, ‘Write\n’, ‘Close’]

The same is displayed as you use readlines() method for displaying the complete content of the file:

objFile = open("python-demo.txt", "r")



print (objFile.readlines())



objFile.close()

The result:

[‘Python\n’, ‘Open\n’, ‘Read\n’, ‘Write\n’, ‘Close’]