Last Updated on April 3, 2021 by admin
Python has an inbuilt function that can create, write, and read files. Python can handle two kinds of files that are normal text files and binary files.
While doing data analysis in Python, the most common thing to do is to open a file and read the content of that file.
The files usually have three common clear methods to read the data in a file. The ‘Read’ method is used for small files which can read all the data into one text string.
How to Read Text File Line By Line by Using Python?
The ‘Readline’ method can read individual line at a time and return them back as strings.
The last method is ‘Readlines’ which can read a file line by line and return them back as a list of strings.
Here we will discuss the easy methods through which you can read a text file line by line from a file by using Python.
Method 1: Use Readlines
You can read the lines in a file one by one by using Python’s readlines function. The readlines function is used in small text files. The Python’s readlines function can read everything in a text file and return them in the form of a list of lines.
- First of all you have to open the file using open () function in read-only mode.
- Then use the file handler to open the file that you want to read by using readlines ().
- You can close the function after reading lines by using close ().
Here are the steps you can follow,
# open the files with read only permit f = open (‘wholeblogs.txt’, “r”) # use readlines to read all lines in the file # the variable “lines” is a list containing all lines in the file Lines = f.readlines () # Close the file after reading the lines f.close ()
Method 2: Use List Function
You can use the list () function and after that open the file handler object to get all the files to line-wise as a list.
- It is a function of just few steps.
# read all lines at once lines = list (f)
In this way, you can easily run over the list of ‘lines’ to analyze lines in small text files.
If the text file is big then you cannot use the list function.
Method 3: Use an Iterator in Python
You use an iterator to read one line at a line in Python. It involves the use of file handler function (FH) and the use of “for” loop.
This method gives the advantage of building a Python function that allows the simplicity of a file using a “for” loop in combination with the iterable object.
fh = open (‘wholeblogs.txt’) for line in fh: # in python 2 # print line # in python 3 Print (line) fh.close()
it is very important to close the opened file in the file handler with the use of the close function “fh, close()”.
The closed file can be opened again by the use of the ‘with’ statement in Python.
Method 4: Use of While Statement in Python
The use of while statement is very important in Python because it can easily handle big files and you do not have to worry about memory problems.
By this method, you can read the lines in a file one by one with readline.
# file handle fh fh = open ('wholeblogs.txt') while True: # read line line = fh.readline () # in python 2, print line # in python 3 Print (line) # check if line is not empty if not line: break fh.close ()
When you open a file you have to close it too by the use of the “with” statement.
If you do not close it then it may cause several bugs in the code.
Read more: TypeError: Must be str, not tuple in Python