Text files are used to store any information in a sequence of lines. The text file is referred to as flat files. In Python, sometimes we need to write, edit or read data from the text file. To do all of these operations, Python provides many inbuilt functions such as “write()” and “read()” to read and write the text file. In this write-up, different methods are explained to read a text file in Python. The following aspect is discussed in this Python guide:
- How to Read a Text File in Python?
- Example 1: Read the Text File Completely
- Example 2: Read the Text File Upto Specific Characters
- Example 3: Read Text File Line by Line
So, let’s start our Python Tutorial!
How to Read a Text File in Python?
In Python, the text file is read using the built-in “read()” function. To read a text file, we need to open it using the “open()” function or to write the file using the “write()” function and then read it. The text file is also read Line by Line using functions such as “readline()”, “readlines()” etc.
The following text file named “itslinuxfoss” will be used to perform the file reading operations.
let’s see different methods one by one that is used to read a text file in Python:
Example 1: Completely Read Text File
The “read()” function reads all the contents of any text file in Python. In the example code, the file’s content is read using the “read()” function. Let’s refer to the below-mentioned code:
Code
file_name = open("itslinuxfoss.txt", "r")
val = file_name.read()
print(val)
In the above code:
- The text file is opened using the “open()” function and saved in the variable named “file_name”.
- The inbuilt “read()” function reads the text file named “itslinuxfoss”.
- The “print” function prints the lines of a text file on the console screen.
Output
The output shows all the content of the text file named “itslinuxfoss”.
Example 2: Read the Text File Upto Specific Characters
We can also read specific characters from the text file in Python. To read the specific character of the text file, the character value is passed inside the parentheses of the “read()” function. Let’s seen an example of code given below:
Code
file_name = open("itslinuxfoss.txt", "r")
val = file_name.read(50)
print(val)
In the above code:
- The “open()” function is used to open the text file and store it in a variable named “file_name”.
- To read the specific characters from the file, we have passed the number of characters “50” which will read the first 50 characters from the text file.
Note: The “read()” function takes only one parameter value and always starts reading characters from the beginning of the text file. We can not read characters directly from the second line using this method.
Output
The output shows that the first “50” characters are read and displayed.
Example 3: Using readline() Function to Read Text File Line by Line
In Python, the text file is read line by line using methods like “readline()”, “readlines()” and “getline()”. This function looks similar to the “read()” function, but the “read()” function reads the complete file at once, and the “readline()” function reads the file line by line. In the following code, the “readline()” function is used to read the text file line by line:
Code
file_name = open("itslinuxfoss.txt", "r")
while(True):
line = file_name.readline()
if not line:
break
print(line.strip())
file_name.close
In the above code:
- The “open()” function opens the text file named “itslinuxfoss”
- “While loop” repeats the “readline()” function until the last line of the text file is reached. When the text file lines reach the end, the “if statement” executes and breaks the sequence of lines.
- The print statement takes the “line.strip()” function as an argument, and the “strip()” function removes all whitespaces of the text file lines.
Output
The output shows that all the lines are read “Line By Line” using the “readline()” function.
That’s all from this guide!
Conclusion
In Python, the “read()” function, “read(n)” with parameter value, and the “readline()” function is used to read a text file completely, specifically and line by line. The simple “read()” function reads all the data of any text file and the “read()” function with parameter value reads specific character values from the text file. The “readline()” along with the “strip()” function read the file line by line until the last line of the text file is reached. This guide has covered different methods used to read a text file in Python.
Saryia is a professional writer with a passion for simplifying complex topics. Specializing in Linux, programming, and technology, Saryia creates in-depth tutorials and articles designed to educate and empower readers.