How to read text files using Python

In this tutorial, I will explain how to read text files using Python built-in functions.

The examples in the coming section show opening a text file, reading a text file, and reading line by line by different modules.

How to open and read a text file?

Before parsing a file in Python program, you need to open it. For that, use the open() function with mode and other optional arguments.

For opening a file in read-only mode, you may use the ‘r’ value for the mode parameter as follows:

open(“tst_read.txt”, “r”)

After opening the file, you may use the read() method for reading the content of the specified text file.  An example below shows opening and reading the content of a text file:

# Opening/Reading a text file content

 ObjRead = open("tst_read.txt", "r")



txtContent = ObjRead.read();

print ("The Content of text file is : ", txtContent)



ObjRead.close()

The output of the above program with our demo text file is:

Python read text file

You can see, the entire content is retrieved by using the read() method and print function displayed it completely.

Reading the text file content line by line

In certain cases, your text file may be too big or you want to get and use/display the content line by line. In that case, you may use the readline() method.

The readline() function reads a single line from the specified file and returns a string that contains a trailing newline character.

The example below shows using the readline() method and displaying the returned result:

#line by line reading



objTxtFile = open("read-demo.txt", "r")

print (objTxtFile.readline());



objTxtFile.close()

The output

Python

 

>>>

You can see, a line break is added in the above result. Our text file contains “Python” as the first line while the second and next lines are:

Python

Open

Read

Write

Close

You may also provide the size argument for getting only a chunk of text from the specified file. For example:

print (objTxtFile.readline(2));

It will output:

Py

>>>

No newline while only the first two characters are returned.

For learning more about Python readline(), go to its tutorial. There, you may also learn how to read the complete file (line by line) using the readline() method.

Reading the content by readlines() method

The readlines() method returns the content of a file in a list. Each line acts as an item in the list with appended newline character.

For making it clearer, the example below used the same text file as in the case of readline() method. The file object is displayed and you can see what it means:

# A demo of readlines()

objText = open('read-demo.txt', "r")



lstLines = objText.readlines()

print (lstLines)

objText.close()

The output:

[‘Python\n’, ‘Open\n’, ‘Read\n’, ‘Write\n’, ‘Close’]

Using for loop for reading text file line by line

The example below uses the open() function and then uses the for loop for parsing the text file line by line.

# line by line reading using for loop



read_for = open('read-demo.txt')

for txt_line in read_for:

    print(txt_line)

read_for.close()

The result with our demo text file:

Python read loop

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!