Read Text files in Python
In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place with an easy example.
Example:
Consider that you want to read a book. To read a book, you are supposed to open it first. Then you can start reading, i.e., perform read operation/activity. When you're done reading, you'll not leave the book open, right? You'll close the book as soon as you complete reading. In the same way, reading of a text file will be performed in python. Let's now go over the detailed procedure for reading a text file.
The procedure followed for reading a text file:
1. Opening the text file:
- We need to use the open () function/method to open the text file.
- The function opens the text file and allows the user to perform any operation.
Syntax of open ()
open (filepath, mode)
As we all know, the characters inside the parenthesis of the method/function are known as Parameters. "filepath" and "mode" are Parameters defined within the method. Here, "filepath" is the file's path where it is stored. You can copy the path of the file when the file is opened, as shown in the figure below.

Mode is defined depending on the operation that you want to perform.
- In order to perform a "read" operation, the mode should be taken as 'r'.
- In order to perform a "write" operation, the mode should be taken as 'w'.
- In order to perform an "append" operation, the mode should be taken as 'a'.
Let's open a text file in a read mode with an example program:
f = open (“D:\downloads hdd\certificates”,’r’)
The text file “certificates” in the “downloads hdd” folder will be accessed and opened with this code.
2. Reading the text file:
- The read operation can be performed on a text file in 3 ways.
- If the given file is of small size and you want to convert all the text into a string as a whole, then the read() method must be used.
Syntax:
var.read()
- If you want to read the text file line after line and return all the lines in a string format, then the readline() method must be used.
Syntax:
var.readline()
- If you want to read all the lines at a time and the text to be represented as a list of strings, then the readlines() method must be used.
Syntax:
var.readlines()
( The variable “var” must be assigned with the "open method” of the text file before calling “read method “. )
4.Closing the text file:
- After reading the text file, the final operation is to close the file by using the close() method.
- It is mandatory to close the file as soon as the work is completed in order to prevent unnecessary access.
- It is safe if you close the files after usage.
- Syntax of close() :
var.close()
- You can close the file without using the close() method also. By using the "with" statement, the text file will be automatically closed.
- Syntax of “with”:
with open(filepath,mode) as var1:
var2 = var1.readlines()
- The variable need not call “close method” now as there is an automatic close reflex “with” to close the file.
Example programs determining opening, reading, and closing operations:
1. Using readline()
f = open (“D:\downloads hdd\certificates”,’r’)
x = f.readline()
y = f.readline()
z = f.readline()
print(x)
print(y)
print(z)
f.close()
Output:
I have achieved a SoloLearn Certificate in Python.
I have achieved CISCO Certificate in Python.
I have achieved a Hackerrank Certificate in Python.
Explanation:
Initially, the file is opened and is assigned to a variable "f". There are 3 lines of text present in the file. To print 3 lines, we should read 3 lines separately (if we use readline() ), assigning that operation to the respective variables "x", "y", and "z". These 3 variables are printed separately.
2. Using read() and “with”
with open (“D:\downloads hdd\certificates”,’r’) as f:
x = f.read()
print(x)
Output:
I have achieved a SoloLearn Certificate in Python.
I have achieved CISCO Certificate in Python.
I have achieved a Hackerrank Certificate in Python.
Explanation:
Initially, the file is opened as f using ”with”. There are 3 lines of text present in the file. To print 3 lines, we read 3 lines as a whole (if we use read() ), considering it as a single content assigning that operation to the respective variable “x”. The variable x is printed. So, 3 lines are printed as a whole.
3.Using readlines()
f = open (“D:\downloads hdd\certificates”,’r’)
x = f.readlines()
print(x)
f.close()
Output:
[‘I have achieved SoloLearn Certificate in Python.’,
‘I have achieved CISCO Certificate in Python.’,
‘I have achieved Hackerrank Certificate in Python.’]
Explanation:
Initially, the file is opened and is assigned to a variable "f". There are 3 lines of text present in the file. To print 3 lines, we read all 3 lines once (if we use readlines() ), considering that the file consists of more than one line and assigning that operation to the respective variable "x". The variable x is printed. The file is closed. So, 3 lines are printed one after another at a time in a list of string format.
This is how the reading of a text file takes place. Depending on the read function we use, the output format varies.