How to read contents of a text file in Python

← Prev

Python offer two built-in methods that you can use to read contents in a text file. The methods are read() and readline(). Python can handle different types of files like a .txt file, a csv file or Excel worksheets etc. Here in this article I am going to show you how to "open" a .txt file in Python and read its content as it is.

Open a file in Python

To read the contents of a .txt file (or any file) in Python, it has to first open the file. To do this you can use the built-in open() function.

Python open() function Syntax

open(file, mode)

The open() function opens a file and returns a file object.

➡️ Ref: open() function

file: The path and name of the file. The file path and name can be "absolute" or "relative" to the current working directory.

mode: A string value defining the "mode" in which the file has to opened. There are different file access modes like,

"r" : open file to read (default)
"w" : open file for writing
"a" : open for writing but appending to the end of the file
"x" : creates a new file (you have provide a file name). it will throw an error a file with the same name already exists.

Read .txt file

As I have earlier mention in this article, you can use two different methods to read contents in a text file.

1) Using "read()" method



Here's the script

f_object = open("C:/Users/Arun/py/example.txt", "r")    # open file in "r" (read) mode.

f_object.close()    # close the text file.

The "open()" function returns a file object. The function has the file path and name, followed by the "r" mode (read mode).

To read the contents in the file, I am using the read() method in the above example. The content is printed in the terminal window.

The "read()" method returns a specified number of bytes (in the form of string) from the "file".

Note: Always use the ".close()" method to close a file.

Read specified number of characters only

You can pass an integer value (a number) as argument (parameter) to the method to get only a specified number of characters from the file. This is "optional". If "no parameter" is passed, it will read all the contents.

For example, if I want to read the first three characters only, I can do this.

print(f_object.read(3))     # read 1st three characters.

2) Using "readline()" method



You can use the readline() method to read the first (or a single) line from the file.

Let us assume, I have three lines of data in the file. To get the "first" line (only), I can use the method like this.

f_object = open("C:/Users/Arun/py/example.txt", "r")
print(f_object.readline())    # read the first line from the file.


If you want to read the first two lines, you have to write the method twice. Like this:

f_object = open("C:/Users/Arun/py/example.txt", "r")
# read first two line from the file.


Like the "read()" method, the "readline()" also takes a parameter (or argument). You can assign a number to the method asking it to return a few characters only. For example, I want to read "10" characters from the "first line" and all the characters from the "second" line.

f_object = open("C:/Users/Arun/py/example.txt", "r")
print(f_object.readline(10))    # read the first 10 characters only.
print(f_object.readline())      # read the second line (all the characters).


Close the file

Never forget to "close" the file after opening and read the contents. To close the file use the close() method. I have used the method in all the examples above.

f_object = open("C:/Users/Arun/py/example.txt", "r")

← Previous