In this post, we will explore 5 ways to read files line by line in Python Sometimes while managing data in Python need to read a text file line by line. Python provides built-in functions readline(),readlines() to read a file .
5 ways to read file line by line in Python
- Using Redline()
- Using While loop
- Using Readlines()
- Readlines() with enumerate()
- Using File Object
1. Readline() to read file line by line in Python
The Python readline() method reads a file line at once and returns it as a string. It returns the next line of the file that contains a newline character in the end. If the end of the file is reached it returns an empty string.
It takes a parameter that represents how many bytes we want to read.
Steps to read Python file line by line
- First, we will open the file using the open() function in reading mode.
- To read a file line by line using Readline() we have used infinite while loop.
- Read the next line on each iteration till it reaches to end of the file.
- If the next line is empty, the loop will terminate using the break statement else we read the next line.
Python Program to read a file
#opening file in reading mode
with open('samplefile.txt','r') as fileobj:
while(True):
line = fileobj.readline()
if not line:
break;
print(line.strip())
Output
This is devenum.com
This is Python file post
Keep exploring
- Read-write CSV files in Python
- Create, Open, Close file in Python
- How to Read a text file in Python
- How to check directory exists in Python
- Ways to create a file if not exist in Python
- How to Create Directory single or nested in Python
- 2 Methods to write text file in Python
- 5 ways to List files in Directory in Python
- 5 ways to append to existing text file Python
2. Readline() with while loop
In the first example, we have used an infinite while loop to loop through the file content. Here we are using a simple while loop.
with open('sampledata.txt','r') as fileobj:
#read each line at once with deadline()
line = fileobj.readline()
while line:
print(line.strip())
line = fileobj.readline()
Output
This is devenum.com
This is Python file post
Keep exploring
3. Readlines() with for loop to read file line by line
The python built-in method readlines() read all lines as a list of strings. So by looping over the list of strings we easily read the file line by line.
Steps to read a file using Readlines()
- First, we will open the file using the open() function in reading mode.
- used the file readlines() method to reading whole file data at once.
- Looping over the contents of the file using for loop.
- Finally printing the result using Print() method.
Python Program to read a file line by line
#opening a file in read mode
with open('sampledata.txt','r') as file_obj:
#reading all lines of file.
Lines = file_obj.readlines()
for line in Lines:
print(line.strip())
Output
This is devenum.com
This is a Python file post
Keep exploring
4.Readlines() with enumerate() to read file line by line
The python built-in readlines() method reads all lines as a list of strings. So we can easily iterate over it using the enumerate() method with for loop as we are doing in this example.
Once we are done reading the file we have to close it using close() method.
Python Program
file_obj = open('sampledata.txt', 'r')
lines = file_obj.readlines()
for index, line in enumerate(lines):
print("Line {}: {}".format(index, line.strip()))
file_obj.close()
Output
This is devenum.com
This is Python file post
Keep exploring
5. File Object to read line by line
The open() function of the file returns an iterable object, So by using the for loop we can loop over each line of the file until file contents exhausted.
Python Program
fileobj = open('sampledata.txt', 'r')
#looping over the file object to read the file
for line in fileobj:
print(line.strip())
#closing the file
fileobj.close()
Output
This is devenum.com
This is Python file post
Keep exploring
Summary :
In this post we have learned 5 ways to read file line by line in Python
- We have used the open() method with mode ‘r’ for reading a file
- Read by using readline() and readlines() methods with for loop, while loop.
- For memory reuse always close the file using the close() method otherwise use WITH a statement to open the file.