Python – Read File Line by Line There are many ways to read a text file line by line in Python. You can read the lines to a list, or just access them one by one in a loop by iterating over the lines provided by some kind of iterator or calling a function on the file object. When we read files in Python, we want to detect empty lines and the file's end. When we call readline we get the next line, if one exists. With this method, we receive an unambiguous result.
Contents
Read Text File in Python
To read text file in Python, follow these steps.
- Call open() builtin function with filepath and mode passed as arguments. open() function returns a file object.
- Call read() method on the file object. read() returns a string.
- The returned string is the complete text from the text file.
Example 1: Read Text File
In the following Python program, we will open sample.txt file in read mode. We will read all the contents of the text file and print the text to the console.
Glyndebourne open house. Python Program
Output
![Python Python](https://mofanpy.com/static/qr/tutorials/python-basic/basic/read-file3_deploy.png)
You can provide the complete or absolute path to the open() function or provide a relative path if the base path is present in the PATH environment variable.
![Python Python](/uploads/1/3/7/5/137510991/855677225.png)
Example 2: Read only Some Characters in the Text File
If you need to read only specific number of characters, say N number of characters, present at the starting of the file, pass N (number) as argument to read() function.
In the following Python program, we will read first 20 characters in the file.
Python Program
Output
read(20) function returned the first 20 characters from the text file.
Example 3: Read file in Text mode
read, write and execute modes are based on the permissions. There are text and binary based on the nature of content. Diffraction grating slits.
In the following example, we will open the file in text mode explicitly by providing “t” along with the read “r” mode.
Python Program
Output
Example 4: Read Text File Line by Line
To read text line by line from a file, use File.readline() function. File.readline() returns the current line and updates its pointer to the next line. So, when you call readline() function for the next time, the next line is returned.
Remember that readline() returns the line along with the new line character at the end of the line, except for the last line. So, if you do not require the new line character, you may use strip() function. There is a catch here. If your line contains white space characters at the start and end, and if you use strip(), you will loose those white space characters in the line.
In this example, we shall read the text file line by line.
Python Program
Output
Summary
In this tutorial of Python Examples, we learned how to read a text file in Python with the help of well detailed Python example programs.
Contents
Python – Read File Line by Line
There are many ways to read a text file line by line in Python. You can read the lines to a list, or just access them one by one in a loop by iterating over the lines provided by some kind of iterator or calling a function on the file object.
In this tutorial, we will learn how to read a file line by line using readline() function, readlines() function, or file object, with the help of example programs.
Example 1: Read Text File Line by Line – readline()
In this example, we will use readline() function on the file stream to get next line in a loop.
Steps to use file.readline() function
Following are the steps to read file line by line using readline() function.
- Read file in text mode. It returns a stream to the file.
- Create an Infinite While Loop.
- During each iteration of the loop, read the next line from the file using readline().
- If the line is not empty, you have the next line. You can check this using if-not. Else, there are no more lines in the file, and we break the loop.
- By the time we break out of the loop, we have read all the lines of file one by one during the iterations.
- Since we are done with the file, we will close it.
Python Program
Output
Example 2: Read Lines as List – readlines()
readlines() function returns all the lines in the file as a list of strings. We can traverse through the list, and access each line of the file.
In the following program, we shall read a text file, and then get the list of all lines in the text file using readlines() function. After that, we use For Loop to traverse these list of strings.
Python Program
Output
Python Readlines Api
Example 3: Read File Line by Line using File Object
In our first example, we have read each line of file using an infinite while loop and readline() function. But, you can use For Loop statement on the file object itself to get the next line in the file in each iteration, until the end of file.
Following is the program, demonstrating how we use for-in statement to iterate over lines in the file.
Python Program
Python Readline Loop
Output
Python Read Method
Summary
In this tutorial of Python Examples, we learned how to read a text file line by line, with the help of well detailed python example programs.