Python Read File Line by Line
In Python, you can read all the lines in a file using different methods. These include readlines (), readline () and context managers. You can read all the text in the files after opening them and perform other operations.
- readlines (): read all lines in a file at once
- readline () with for loop
- readline () with while loop
- Context managers
1. Readlines (): Read All Lines in a File at Once
The readlines () method is the most popular method for reading all the lines of the file at once. This method reads the file until EOF (End of file), which means it reads from the first line until the last line. When you apply this method on a file to read its lines, it returns a list. This list contains all the lines of the file. Here, each line within the list is a list object.
The syntax of the method is as follows:
file.readlines (num_line)
Here, the num_line parameter specifies the number of lines or number of bytes that have to be read from the file. But this parameter is optional. It takes an integer value. If the returned bytes exceed the number specified in the hint, the method will stop returning lines. The default value for this method is -1, which means all the lines within the file will be returned.
For example,
# Python 3 Code
# Python Program to read file line by line
# Open file
mf = open("myfile.txt", "r+")
print("File to read: ", mf.name)
# Read all lines in the file
file_content = mf.readlines()
# Print all lines in file
print("Read Line: ", file_content)
# Close opened file
mf.close()
Output:
File to read: myfile.txt
Read Line: ['Line one\n', 'Line two\n', 'Line three\n', 'Line four\n', 'Line five']
The readlines () method is a very good option for reading all the contents of a small file. But you might face problems while working with big files.
2. Using "While" Statement
Now we will look at how you can read the lines of a file one by one using a while statement. If you are working with a large file with a lot of text, it is best to use the readline () method. This method fetches the lines one by one instead of retrieving all the text at one go.
Example,
# Python 3 Code
# Python Program to read file line by line
# Using while statement
# Open file
mf = open("myfile.txt", "r+")
print("File to read: ", mf.name)
# Read single line in file
file_line = mf.readline()
# use the readline() method to read further.
# If the file is not empty keep reading one line
# at a time, till the file is empty
while file_line:
print(file_line)
# use realine() to read next line
file_line = mf.readline()
# Close opened file
mf.close()
Output:
Line one
Line two
Line three
Line four
Line five
Another example of the while statement is:
# Python 3 Code
# Python Program to read file line by line
# Using while statement
# Open file
mf = open("myfile.txt", "r+")
print("File to read: ", mf.name)
while True:
# Read single line in file
file_line = mf.readline()
print(file_line)
if not file_line:
break
# Close opened file
mf.close()
Output:
Line one
Line two
Line three
Line four
Line five
In the above code, the while statement checks for a Boolean value to be True. The readline () method reads the text line by line. When it reaches the end of the file, the execution of the while loop stops.
3. Using "for" Loop
You can read the lines of a file in Python using a for loop. Use the following code for it:
# Python 3 Code
# Python Program to read file line by line
# Using while statement
# Open file
mf = open("myfile.txt", "r+")
print("File to read: ", mf.name)
for file_line in mf:
# Print single line
print(file_line)
# Close opened file
mf.close()
Output:
Line one
Line two
Line three
Line four
Line five
Here, mf is the file handler that is used for opening the file called myfile.txt. A for loop is executed on every line in the text file. file_line the variable used for iterating through the loop.
4. Using Context Manager
If you are opening a file for some operation, you need to close it too. In case you don’t close it, it will automatically be closed when the function you are using to handle it completes execution. This happens when the last reference of the file handler is destroyed. But there may be a situation where the programs are long and the function will not complete execution soon.
In that case, you have to make use of a context manager in Python. This manager will perform all the tasks such as closing files for you - even if you forget to do it.
Take a look at this program,
# Python 3 Code
# Python Program to read file line by line
# Using Context Manager
List_file_lines = ["Apple", "Orange", "Banana", "Mango"]
# Define function to create file
def create_file():
with open ("testfile.txt", "w") as wf:
for line in List_file_lines:
# Write all lines
wf.write(line)
wf.write("\n")
# Define function to read files
def read_File():
rf = open ("testfile.txt", "r")
# Read file lines
out = [] # list to save lines
with open ("testfile.txt", "r") as rf:
# Read lines using for loop
for line in rf:
# All lines and strip last line which is newline
out.append(line.strip())
return out
# Define main function to create and read file
def main():
# Create test file
create_file()
# Read lines from testfile.txt
outList = read_File()
# Iterate over the lines
for line in outList:
print(line.strip())
# Run mail function
if __name__ == "__main__":
main()
Output:
Apple
Orange
Banana
Mango
Here the with clause depicts the concept of context managers. This keeps track when you are opening the file and closes it as soon as the function ends.
Another example of context managers along with a while loop is as follows:
# Python 3 Code
# Python program to read file line by line
# Using context manager & while Loop
with open ("myfile.txt", "r") as rf:
# Read each line of a file in the loop
for line in rf:
# Print every line in a file except the last line which is newline \n
print(line.strip())
Output:
Line one
Line two
Line three
Line four
Line five
Conclusion
The method you will use for reading the lines of a file will depend upon the type of file you have. If you have a small file with a few lines of text, readlines () method is the best way to go. Using a for loop is also good for small files. For larger files, this method is not very memory efficient. So, in that case, you can use the while statement along with with the readline () method.
Context managers will make sure that you can read all lines of a file without worrying about closing the file handler.