Python Read Files

As discussed in the python file opening, the built-in open() function is the key function to work with the files. To perform any file-related operations such as creating the file, writing the file, reading the file, etc., you need to use the open() function with different modes such as x, w, a, r, etc. in python.

 

First, you need to open the file in read (r) mode using the built-in open() function to read the file content. After that, you need to use read methods to read the data from files.

 

In python, we have different methods to read the data from files.

 

MethodDescription
read() It is useful to read and return the complete text of the file.
readline() It is useful to read and return the single-line text of the file.
readlines() It is useful to read all lines of text and return a list object.

Read Data from Files

Using the read() method, you can read the complete content of the file in python. Following is the example of reading the data of files using the read() method in python.

 

file = open("testfile.txt", "w")
file.write("Hi\n")
file.write("Welcome to Tutlane")
file.close()

#Open and read the file content
file = open("testfile.txt", "r")
print(file.read())
file.close()

If you observe the above example, we created a file (testfile.txt) and wrote the required text. After that, we used the read() function to read the content of the file.

 

It is always good practice to close the file after writing or reading the data from a file. In some cases, the file data will not update until we close the file.

 

The above example will return the result as shown below.

 

Hi
Welcome to Tutlane

If you observe the above result, the read() function has returned the file's whole text.

 

In the above example, we created a file in the same python location because we didn't mention any file path. If you want to read the data from a file that exists in a different place, you need to specify the file path as shown below.

 

#Open and read the file content
file = open("D:\\testfile.txt", "r")
print(file.read())
file.close()

Read Specified Characters from the File

By default, the read() function will return the whole text of the file. Instead of reading the entire text of the file, you can specify the number of characters you want in the read() function.

 

Following is the example of reading the specified number of characters from the file in python.

 

#Open and read the file content
file = open("testfile.txt", "r")
print(file.read(7))
file.close()

When you execute the above example, it will return the result as shown below.

 

Hi
Welc

As we have \n character in the first line of text, we are able to see 6 characters in the result.

Read Lines from File

Using the read() function, we can read the whole text or a specified number of characters from the file. If you want to read a single line of text, you need to use the readline() method. 

 

The readline() method will return the first line of text from the file. Following is the example of reading a single line of text using the readline() method in python.

 

#Open and read the file content
file = open("testfile.txt", "r")
print(file.readline())
file.close()

The above example will return the result as shown below.

 

Hi

If you call the readline() method multiple times, it will return the multiple text lines from the file.

 

#Open and read the file content
file = open("testfile.txt", "r")
print(file.readline())
print(file.readline())
file.close()

The above example will return the first two lines of text from the file as shown below.

 

Hi
Welc

If you want to read all the text lines from the file as a list, you need to use the readlines() method as shown below.

 

file = open("testfile.txt", "r")
print(file.readlines())
file.close()

The above example will return the file content as shown below.

 

['Hi\n', 'Welcome to Tutlane']

Iterate through the File Content

In python, you can iterate through the file content line by line using for loop.

 

Following is the example of looping through the file content line by line in python.

 

file = open("testfile.txt", "r")
for line in file:
    print(line)

The above example will return the result as shown below.

 

Hi

Welcome to Tutlane

You can also use a while loop to iterate through the file content line by line as shown below.

 

file = open("testfile.txt", "r")
line = file.readline()
while line != "":
    print(line)
    line = file.readline()
file.close()

 

Instead of using for loop and while loop, you can also use file object inbuilt iterator to loop through the file content. Following is the example of reading the file content line by line using the file object inbuilt iterator.

 

file = open("testfile.txt", "r")
while True:
    try:
        line = next(file)
        print(line)
    except StopIteration:
        break
file.close()

The above example will return the result as shown below.

 

Hi

Welcome to Tutlane

This is how you can read the data from files using different methods based on your requirements.