Python is a robust programming language with built-in support for reading and writing files. In this article, we'll go over the various approaches to reading a text file line-by-line in Python. But first we have to revise some concepts about reading text files in general.
How to Read a .txt File in Python?
We need text files for many reasons and so, each coder should know how to operate them. We use the open() function to read a text file in Python. This function is used to open a file and returns a file object. It can have two arguments: the file name or path and the mode in which to open the file. Here is the simple syntax for this operation.
Here 'example.txt' is the file name that we want to open, and 'r' is the mode that signifies that we want to read the file.
The other modes available are as follows:
- Read Mode (‘r’): It simply reads the contents of a file.
- Write Mode (‘w’): If your file already exists, this will overwrite the content of your file. Otherwise creates a new file.
- Append Mode (‘a’): It appends the content in an already existing file instead of overwriting, If a file does not exist It creates a new file.
Now that we have a fundamental understanding of how to open and operate a file, Here is a simple example:
A demo file called "example.txt" that contains some text about Python was created, and its contents were read using the read() function. As you can see, the output includes the entire content.
How to read a Text File line-by-line in Python?
Sometimes, reading a text file is not enough and we need to separate each line in the content of the file, so we have a great function by Python for this purpose.
We can read the file line by line in Python using a while loop and the readline() function. With the readlin() function called on the file, we read each single line of text, storing it in the line variable. We end the loop if the line variable is empty because that indicates that the file has ended. If not, the line is printed.
Please keep in mind that this function keeps track of its current position in the file. That's why we need a loop to iterate over the file and read the next line of text from the file.
The key distinction between the two approaches is that the read() function reads the entire file in a single pass and disregards any available sentence spaces, whereas the readline() function reads a single line in a single pass, from the first character to the next available sentence space, prints it, and repeats the process.
However, you should also know about the readlines() method. With the help of this, we can quickly read every line in a text file while maintaining the sentence breaks. The readlines() functions reads every line and stores it in the variable lines in this case. Then, display each line after iterating through the lines using a "for" loop.
How it works is that the entire file is read into memory as a list of strings, which can be inefficient for large files. So, it is more memory-efficient to use a loop with the readline() method.
How to Read a File into a List?
What is actually happening here is that Python is maintaining a list with every line of the file's content as an element, and then using the 'for' loop we iterate over the list to print individual lines. As you can see in the previous method, readlines() stores all the lines in a variable. Therefore, we can simply print the list variable if we want a list of the information. For Example:
There is an alternative method that uses the strip() function, as shown below:
Using a "with" block, the code reads the contents of a file called "example.txt" in read-only mode ('r'). The following step involves reading each line from the file, using the "strip()" method to eliminate any leading or trailing white space characters, and adding the resulting line to a list called "lines". The list of lines that were stripped is then printed.
It is very easy to read a line-by-line in Python using readline method that you learned above. If you have an assignment that is related to this problem, we provide assignment help in Python to assist you with that. Happy Learning:)