How to Read Text Files In Python, With Inbuilt Functions | 2022

There are several ways to read text files in Python, including using the open () function using the access modes provided by Python and the by importing Pandas library and Numpy library.

What Is a Text File?

Text files are simply collections of sentences containing words, numbers, and any special symbols in any electronic device such as mobile phones, laptop computers, and desktop computers.

In general, text files are created on desktop computers using notepad and Wordpad.

When we save a file in notepad or Wordpad, it is automatically saved in text format; however, if you are using another app, use .txt at the end of the file name to save it as a text file.

Python Inbuilt Function to Read Text Files

Open() is a built-in method in Python that allows you to read text files. There are 6 modes to read and write in this open () function.

file operation modes in python
file operation modes in python
S.No.ModeFunction
1 ‘r’It is used for opening the file in read-only mode. In this mode, we can only read a file if it exists.
2‘r+’It is used for opening a file in read-and-write mode. In this mode, we can write in the existing file along with reading the file.
3‘w’This mode is used to open files in write-only mode. We can only write in the file if the file doesn’t exist it creates a new one otherwise previous data is automatically replaced by new data.
4‘w+’This mode is used to open file write along with read mode. In this mode, we can read files along with writing in the existing file by replacing previous data automatically replaced.
5‘a’This mode is used to append the existing file. In this mode, new text written is automatically added after the end of the last line in an existing file.
6‘a+’This mode is used to append the existing file along with read mode. In this mode, we append a file and we can read that particular existing file. If the file doesn’t exist then it automatically creates a new one.
open ( ) modes

Data Preparation

Here is the information that will be used throughout the entire article, along with its position.

File in different Directory :

How to Read Text Files In Python, With Inbuilt Functions | 2022 1
File name and file location
How to Read Text Files In Python, With Inbuilt Functions | 2022 2
File Text

The images on the top two show the location of the file, its name, and the data that is written in the text file. In this location of the Python ide which we are going to use is not in the same directory i.e.location of the python ide and text file are different.

File in the same Directory :

How to Read Text Files In Python, With Inbuilt Functions | 2022 3
File name and file location
How to Read Text Files In Python, With Inbuilt Functions | 2022 4
File text

The images on the top two show the location of the file, its name, and the data that is written in the text file. This location of the Python IDE that we are going to use is in the same directory i.e.location of the python ide and the text file is the same.

Read Function in Python

It follows syntax File_object = open("File_Name.txt","Access_mode"). In this, we will simply open the file and read using the read() function.

Code:

# open() to open beetechnical2 text file
# same directory as ide
file1 = open("beetechnical2.txt","r")

#use read() to read the text in ide
print(file1.read())

#open() to open beetechnical text file
#different directory as ide
file2 = open("C:/Users/ACS/Desktop/beetechnical.txt","r")

#using read() to read the text in ide
print(file2.read())

Output :

How to Read Text Files In Python, With Inbuilt Functions | 2022 5
The output of the text file
  • To read data text files, we used open() with access mode “r” read-only in the preceding code.
  • file1 object was used to open a file in the same directory as our Python IDE (jupyter), and function read () was used to read text from the text file. In the same directory, we only have to add the file name .txt to access any mode.
  • file2 object was used to open a file that was in a different directory than our Python IDE (jupyter) and function read () was used to read text from a text file. In different directory cases, we have to add the whole location of a text file to open it in IDE.

Open Text File in Python Using Open Method

In this code read the text file using iteration in this case. It will read a text file line by line and save the results to a variable object. There are also several methods for reading text files after opening a text file, including read(), readline (), and strip ().

Code :

# open() to open beetechnical2 text file
# method 1 to read file
with open("beetechnical2.txt","r") as file :
    data = file.readline()

# print text data    
print(data)


# method 2 to read text file
file2 = open("C:/Users/ACS/Desktop/beetechnical.txt","r")

for data in file2 :
    print(data.strip())               # strip () to read text file in more efficient way 

Output :

How to Read Text Files In Python, With Inbuilt Functions | 2022 6
Output for a text file
  • In the preceding Python code, we use loops to read text files.
  • We used the readline( ) function in one method to read text file lines one by one. As readline( ) only reads one line at a time, readlines( ) reads all lines, and deadlines (n) reads only n lines in a text file.
  • In method 2, we use the strip( ) function to read text lines because it automatically removes blanks line if they are present.

Append data to an existing text files in Python

In this code, we will read a text file by creating a new one by opening it in write mode “w,” because if the file already exists, the previous text will be replaced, so this is a new text file in any case.
Then will append to see what changes it makes with mode “a.”

Code :

# Python program to illustrate
# All access mode of open ( )

# to create a new file beetechnical 4
file1 = open("beetechnical4","w+")
data = "This is a  tech site \n This site name is Beetechnical \n There are many other article then tech site \n" 
#file1.writelines(data)
%store data >beetechnical4.txt
file1.close()

# output of new text file
file1 = open("beetechnical4.txt","r")
print(file1.read())
file1.close()


  
# Append-adds at last
file1 = open("beetechnical4.txt","a")
file1.write("It is source of passive income also")
file1.close()
  
# output after appending    
file1 = open("beetechnical4.txt","r")
print(file1.read())
file1.close()
  

Output :

How to Read Text Files In Python, With Inbuilt Functions | 2022 7
Output after writing and append
  • The code above uses the mode “w+” to read a text file after it has been updated with new strings. then, using %store, we write the string into a text file.
  • Then, using mode “a,” we added our most current file. Then we used open () to open the file and read it to check for changes.
  • As demonstrated by our output, the append function appends strings to the end of earlier strings.

Create Text File Along With Folder in Python

This is an entirely new method in which we will create a folder and a text file within it. Using the magic word %mkdir, we create a folder.
To save a string in a text file, use the %store magic word. The magic word %cd stands for location.

Code :

# text we want to store in our  text file 

data = '''This is a  tech site
This site name is Beetechnical
There are many other article then tech site
'''


%mkdir Ayami        
%cd Ayami
%store data >beetechnical3.txt 
%cd .. 

# open () to open file
file = open("Ayami/beetechnical3.txt", "r")  

for line in file:
    print (line)
    
    
# will close file as soon as code ends    
file.close()

Output :

How to Read Text Files In Python, With Inbuilt Functions | 2022 8
Output for folder
  • In the preceding code, we used %mkdir add to create a folder in the same directory as the IDE, and then we declared a string named data, which contains some lines.
  • Following that, we created a text file called beetechnical3 and saved our string in it.
  • %cd indicates the location of the file, and %cd Ayami indicates the location of the folder Ayami.
  • Then, similarly to other programs, open a file and store a string in it, then read it with “r” and print the string. Below is the output of created folder
How to Read Text Files In Python, With Inbuilt Functions | 2022 9
The output of created folder

Conclusion :

We covered how to read text files in Python without importing any libraries in this tutorial. Python’s open () function is a mandatory function used to read text files.

After opening text files, we also used the functions read ( ), readline(), readlines(), and strip() to read the files.
We occasionally need to read files when working with data in order to make changes, therefore learning how to read text files in Python or any other language can help us read them in primary memory.

Scroll to Top