How to Read a text file in Python

In this article, we will learn about How to Read a text file in Python.

To perform a read or write operation on a file the file must be in the open state. If the file is not opened then it is not possible to do the read and write operation. Similarly, after we are done with the read-write operation, then it is highly recommended to close the file for proper file data flushing.

Steps to read a text file in Python


  • Open a Text File.
  • Read a Text File.
  • Close a text File.

Open a File for Reading


The File built-in Open() function is used to open files in writing mode. It takes multiple arguments. The main is the first parameter FilePath and the second is an optional file mode.

Syntax

file_open = open("file_path","mode")
  • File_path : It specify the path/location where we are creating a new file.
  • Mode: It specifies the type of operation performing on file.

Using file object

fileobj = open("myfile","r")

using with keyword

Another short syntax to open and close the file by using with keyword

with open('file_path') as file_open:

Once we have done with writing the data into file good practice to close the file to free the memory space

file_open.close()

Another short syntax to close and open file by using the with keyword

with open('file_path') as file_open 

List of Mode for open a file for reading in Python


These are the list of mode in Python

Mode Details
ropen file for reading plain text
r+open file for writing and reading plain text
rbopen file for reading binary data

Python Methods to Reading from a Text File?


We already have the file open now, so next, we will learn is how to read the content of the file. To read the content of the file we will be using the read() function which reads the content of the file. when we open the file by using the open() function it returns a file object. This object has many functions implemented in it. read() function is one of those functions.

In Python There are three ways to read from a text file

  • read(): This function returns the file specified N number of bytes from the file. Default value is -1 mean entire file contents.
  • readline(): It returns one line from the file.
  • readlines(): It returns the list that contains the line of the file as a list element.

1. Using Read() method Read the entire Text file


In this example we are reading the entire content of file using the read() method.

file_open = open("samplefile.txt","r")
file_contents = file_open.read()

print(file_contents)

2. Using readline() method to Read a line from a Text file


In this example we are reading a line from a text file using the in-built function readline()

file_open = open("samplefile.txt","r")

Line1 = file_open.readline()
Line2 = file_open.readline()
print(f'line 1: {Line1}')
print(f'line 2: {Line2}')
file_open.close()

3. Readlines() to Read Text file line by line


In this example we are reading a text file line by line using the in-built function readlines() and for loop printing file contents line by line.

with open('samplefile.txt','r') as file_open:

 
 Lines = file_open.readlines()
 
 for line in Lines:    
    print(line)