Python 3 – File readline() Method
File handling is an important concept in any programming language, as it allows us to read and write data from files. Python, being one of the most popular programming languages, has several built-in functions and methods for file handling. One such method is the readline()
method, which is used to read a single line from a file.
Overview
The readline()
method reads a single line from the file and returns it as a string. The method has an optional parameter – size
– which specifies the maximum number of bytes that should be read in a single line. If no parameter is passed, the method reads the entire line until it encounters the newline character (\n).
Syntax
file.readline(size)
Parameters
size
(optional): Maximum number of bytes to be read in a single line. If no value is provided, the entire line is read.
Return Value
- The method returns a string containing the text read from the file.
Example 1: Reading a Single Line from a File
Let’s take a look at an example that reads a single line from a file named example.txt
using the readline()
method.
# Open file for reading
file = open('example.txt', 'r')
# Read first line from file
line = file.readline()
print(line)
# Close the file
file.close()
Output:
This is the first line of example.txt!
In the above example, we first opened the file example.txt
in read mode using the open()
method. We then used the readline()
method to read the first line from the file and assigned it to the variable line
. Finally, we printed the content of line
to the console and closed the file.
Example 2: Reading Multiple Lines from a File
Let’s take a look at another example that reads multiple lines from a file.
# Open file for reading
file = open('example.txt', 'r')
# Read lines from file until EOF
while True:
line = file.readline()
if not line:
break
print(line)
# Close the file
file.close()
Output:
This is the first line of example.txt!
This is the second line of example.txt!
This is the third line of example.txt!
In the above example, we first opened the file example.txt
in read mode using the open()
method. We then used a while
loop to read multiple lines from the file until the end of the file is reached.
Inside the while
loop, we used the readline()
method to read a single line from the file and assigned it to the variable line
. We then checked if line
was empty (i.e., the end of the file has been reached) using the not
operator. If line
was empty, we broke out of the loop.
Finally, we printed the content of line
to the console and closed the file.
Example 3: Reading a Fixed Number of Bytes
Let’s take a look at an example that reads a fixed number of bytes from a file.
# Open file for reading
file = open('example.txt', 'r')
# Read first 10 bytes from file
line = file.readline(10)
print(line)
# Close the file
file.close()
Output:
This is th
In the above example, we first opened the file example.txt
in read mode using the open()
method. We then used the readline()
method to read the first 10 bytes from the file and assigned it to the variable line
. Finally, we printed the content of line
to the console and closed the file.
Conclusion
In this article, we discussed the readline()
method in Python, which is used to read a single line from a file. We also looked at some examples that demonstrated how to use the method to read a single line, multiple lines, and a fixed number of bytes from a file. The readline()
method is an essential method when it comes to file handling in Python, and knowing how to use it effectively can make your code more efficient and readable.