Reading a file in Python line by line can be done by using the readline() and readlines() methods.
What are the two ways to read file line by line in Python?
Reading a file in Python can be done easily using the built-in open() function.
There are two ways to read file line by line in Python.
- The readline() method
- The readlines() method
Before using any of these two methods, first, you have to open the file.
Opening a file in Python
To open a file in Python, you can use the built-in open function.
This function takes the file name and access mode as an argument.
Syntax
File_object = open("File_Name", "Access_Mode")
You can read in detail about the open() function and access modes here.
The open function will return an object of the file.
You can use this object to read and manipulate the file.
Let’s try to open a file.
f = open("main.txt", "r")
contents = f.read()
print(contents)
Output
This is line 1.
This is line 2.
This is line 3.
This is line 4.
Using the read method, you can read the entire content of the file.
How do you read a file line by line in Python?
To read a file line by line in Python, you have two methods available.
- The readline() method
- The readlines() method
1: The readline() method
The readline method will return one single line at a time.
Syntax
file.readline(n)
The ‘n’ is an optional argument to the readline() method.
You can pass the required number of characters as an argument ‘n’.
Code
f = open("main.txt", "r")
content = f.readline()
print(content)
Output
This is line 1.
Let’s say you want the first four characters of this line.
You can pass 4 as an argument to the readline method, such as readline(4).
f = open("main.txt", "r")
content = f.readline(4)
print(content)
Output
This
2: The readlines() method
The readlines() method will return a list.
This list will have each line of the file as the list item.
f = open("main.txt", "r")
content = f.readlines()
print(content)
Output
[‘This is line 1.’, ‘This is line 2.’, ‘This is line 3.’, ‘This is line 4.’]
Now you have got the list.
You can implement all the list operations on this list.
You can access each line by iterating through the list.
Code
f = open("main.txt", "r")
content = f.readlines()
for everyline in content:
print(everyline)
Output
This is line 1.
This is line 2.
This is line 3.
This is line 4.
Or you can access the required line by its index.
f = open("main.txt", "r")
content = f.readlines()
print(content[0])
print(content[2])
Output
This is line 1.
This is line 3.
What is the difference between Readlines and readline in Python?
Both readline() and readlines() methods are used to access the lines of a file in Python.
The readline() method returns a single line of the file.
Whereas the readlines() method returns a list with each line as a list item.
What is the difference between read () and Readlines () in Python?
The read() method returns the whole content of the file that you want to read.
Whereas the readlines() method is used to access the individual lines of the file.
Whereas the readlines() method returns a list with each line as a list item.
How to turn the lines from a text file into a list in Python?
You can use the readlines() method to turn the lines from a text file into a list.
Then you can apply all the list operations to access and manipulate the data of each individual line.
In summary, reading a file in Python line by line can be done by using readline() and readlines() method.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.