Python File readline() Method – Tutorial with Examples

Python File readline() Method

The readline() method in Python is a built-in method used to read a single line from a file. It reads until it encounters a newline character (‘\n’) or reaches the end of the file. If the optional argument size is specified, it reads at most that many characters.

Syntax

file.readline(size)

size (optional) – The maximum number of characters to read. If size is not specified, it reads until the end of the line.

Return Value

The readline() method returns a string containing the contents of the line, including the newline character if present. If the end of the file has been reached, an empty string is returned.

Examples

Example 1: Read a single line from a file

In this example, we will read a single line from a file using the readline() method:

file = open("example.txt", "r")
line = file.readline()
print(line)
file.close()

In this example, we opened a file named “example.txt” in read mode, read a single line from the file using the readline() method, printed the line to the console, and then closed the file.

Output:

This is the first line of the file.

Example 2: Read a single line with a specified size

In this example, we will read a single line from a file with a specified size using the readline() method:

file = open("example.txt", "r")
line = file.readline(10)
print(line)
file.close()

In this example, we opened a file named “example.txt” in read mode, read a single line with a maximum size of 10 characters using the readline() method, printed the line to the console, and then closed the file.

Output:

This is th

Example 3: Read all lines from a file

In this example, we will read all the lines from a file using the readline() method in a loop:

file = open("example.txt", "r")
line = file.readline()
while line:
    print(line)
    line = file.readline()
file.close()

In this example, we opened a file named “example.txt” in read mode, read all the lines from the file using the readline() method in a loop, printed each line to the console, and then closed the file.

Output:

This is the first line of the file.
This is the second line of the file.
This is the third line of the file.

Use Cases

The readline() method can be useful in situations where we need to read a single line from a file or read multiple lines from a file one at a time. It can also be useful for processing large files, as it reads the file one line at a time and does not load the entire file into memory.

Conclusion

The readline() method in Python is a useful built-in method for reading a single line from a file. It can also be used in a loop to read multiple lines from a file one at a time. The optional argument size can be used to specify the maximum number of characters to read. The method returns a string containing the contents of the line, including the newline character if present. The readline() method is particularly useful for processing large files, as it reads the file one line at a time and does not load the entire file into memory.

Leave a Reply

Your email address will not be published. Required fields are marked *