Python File readlines() Method – Tutorial with Examples

Python File readlines() Method

The readlines() method in Python is a built-in method used to read all the lines from a file and return them as a list. It reads until the end of the file and returns a list of strings, where each string represents a single line from the file.

Syntax

file.readlines(sizehint)

sizehint (optional) – The maximum number of bytes to read. If sizehint is not specified, it reads all the lines from the file.

Return Value

The readlines() method returns a list of strings, where each string represents a single line from the file.

Examples

Example 1: Read all lines from a file

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

file = open("example.txt", "r")
lines = file.readlines()
print(lines)
file.close()

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

Output:

['This is the first line of the file.\n', 'This is the second line of the file.\n', 'This is the third line of the file.\n']

Example 2: Read a specific number of lines from a file

In this example, we will read a specific number of lines from a file using the readlines() method:

file = open("example.txt", "r")
lines = file.readlines(2)
print(lines)
file.close()

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

Output:

['This is the first line of the file.\n', 'This is the second line of the file.\n']

Example 3: Strip the newlines from the lines

In this example, we will read all the lines from a file using the readlines() method, and strip the newline characters from the end of each line:

file = open("example.txt", "r")
lines = file.readlines()
stripped_lines = [line.strip() for line in lines]
print(stripped_lines)
file.close()

In this example, we opened a file named “example.txt” in read mode, read all the lines from the file using the readlines() method, stripped the newline characters from the end of each line using a list comprehension, printed the stripped lines 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 readlines() method can be useful in situations where we need to read in a large amount of text data from a file and process it line by line. It can also be useful when we need to extract specific lines from a file or perform text manipulation on the lines of a file. Additionally, the ability to specify the sizehint parameter can be helpful when we only need to read a certain amount of data from a large file.

Conclusion

The readlines() method is a powerful tool for reading and processing text data from a file in Python. By providing the ability to read all the lines from a file or a specific number of lines, and the ability to strip newline characters from the end of each line, this method can be used in a variety of use cases for text manipulation and data processing.

Leave a Reply

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