Python provides different ways in order to read a file line by line. Line by line reading a file can be useful if the file is very large and can not be stored in the memory completely to make the read operation more efficient and faster. Reading files chunk by chunk is a good way which can be also expressed by reading line by line.
Open File To Read
Before reading a file line by line we will open the file for the read operation. A file can be opened with different modes like for reading, writing, appending, reading binary data, writing binary data. In this case, our intention is to read the file in which mode is r
. We will use open()
function where we will provide also r mode options and the file name or path we want to read.
#!/bin/python try: fp = open('myfile.txt') # We will read "myfile.txt" line by line # here
Here we can see that we will open the file and set the file handler fp
then we will read line by line in the next sections. We will use try
in order to catch exceptions and errors.
Close Opened File Properly
Opening a file will allocate some resources on the systems and we should free this resource after the operations are complete. We need to close the file properly when the reading of line by line is complete. We can close the opened file with the close()
method by providing the file handler. We will also use close() method inside the finally
part to the try
if there is an exception about opening and reading files we will close this file.
#!/bin/python try: fp = open('myfile.txt') # We will read "myfile.txt" line by line # here finally: fp.close()
Read Single Line From File
Python provides 3 methods in order to read a file. read()
function will read the whole file and return the content. This can be useful small files but it is very error-prone while working with big files. readline()
function is the most useful function to read file line by line. readline()
function will read a single line from the file and jump the cursor next file for next readline() function call. So for each readline() call the cursor of position is stored by the file handler. In the following example, we will just read a single line from the file myfile.txt with fp file handler and then close the file.
#!/bin/python try: fp = open('myfile.txt') # We will read "myfile.txt" line by line line = fp.readline() print(line) finally: fp.close()
Read File Line By Line with readline() Method
In this case, we will read the complete file named myfile.txt line by line by using readline() method. We will use while
loop and some checks with if
condition keyword. We will create an infinite loop with while True:
line where we will read single line in every step. In the bottom, we will check whether the file end and there is no line to read with if not line:
line and if the file is ended we end the loop with break
keyword.
#!/bin/python try: fp = open('myfile.txt') # We will read "myfile.txt" line by line while True: # Read current line and put content to line line = fp.readline() #Print the line print(line) #If there is no line exit from loop if not line: break finally: fp.close()
Read File Line By Line with For Loop
We can also use for
loop in order to read a file line by line. We will open the file named myfile.txt
and iterate over the lines with for line in
fp` file handler. When there is no line to read the for loop will end.
#!/bin/python try: fp = open('myfile.txt') # We will read "myfile.txt" line by line for line in fp: #Print the line print(line) finally: fp.close()
Read File Line By Line with While Loop
We can also use while
loop in order to read line by line. We will use the readline()
function. We will read a single line from the file in every iteration and set to variable line
and check if we have reached the end of the file.
#!/bin/python try: fp = open('myfile.txt') # We will read "myfile.txt" line by line #Read the first line line = fp.readline() while line: #Print the line print(line) #Read next line line = fp.readline() finally: fp.close()
Convert File Lines Into List
In some cases converting the given file lines into the list will be very beneficial. Hopefully, Python provides the readlines()
method in order to read all lines from given file and then return as a list where each line is an item in the given list. In this example, we will read the file named myfile.txt
and return lines as a list named lines
. We can print a specific line from the list by providing the exact index number.
#!/bin/python try: fp = open('myfile.txt') # We will read "myfile.txt" line by line #Read the first line lines = fp.readlines() print(lines[0]) print(lines[1]) print(lines[2]) print(lines[3]) finally: fp.close()