Python – Read File as String

You can read whole content of a file to a string in Python.

In this tutorial, we will learn how to read a file to a string, covering different scenarios, with the help of well detailed examples.

Generally, to read file content as a string, follow these steps.

  1. Open file in read mode. Call inbuilt open() function with file path as argument. open() function returns a file object.
  2. Call read() method on the file object. read() method returns whole content of the file as a string.
  3. Close the file by calling close() method on the file object.

The default mode is text mode for open() function. So, even if you do not provide any mode for open() function, the read operation should work fine.

Example 1 – Read File to a String

In this example, we assume that we a file with two lines of content present in the location D:/data.txt. We shall apply the sequence of steps mentioned above, and read the whole content of file to a string.

example.py – Python Program

#open text file in read mode
text_file = open("D:/data.txt", "r")

#read whole file to a string
data = text_file.read()

#close file
text_file.close()

print(data)

Run the above program. Python interpreter reads the file to a string and prints it to the standard output.

Output

Hello World!
Welcome to www.tutorialkart.com.

Example 2 – Read File to String – File Path Incorrect

In this example, we assume that we are trying to read content of a file that is not present. In other words, file path is incorrect.

example.py – Python Program

#open text file in read mode
text_file = open("D:/data123.txt", "r")

#read whole file to a string
data = text_file.read()

#close file
text_file.close()

print(data)

Run the above program. As the file is not present, we should expect that Python interpreter might throw some Error.

Traceback (most recent call last):
  File "d:/workspace/fipics/rough.py", line 2, in <module>
    text_file = open("D:/data123.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'D:/data123.txt'

There is our FileNotFoundError. And the message says that no such file or directory with the file path passed to open() function.

Example 3 – Read File to String – Prior Check if File is Present

In this example, before reading a file, we shall check if the file present. Only after we make a confirmation that the file present, we shall read its content to a string.

To check if a file is present, we use os.path.isfile() function.

example.py – Python Program

import os

file_path = "D:/data123.txt"

#check if file is present
if os.path.isfile(file_path):
    #open text file in read mode
    text_file = open(file_path, "r")

    #read whole file to a string
    data = text_file.read()

    #close file
    text_file.close()

    print(data)

Now, try with the file paths that is present and not present. If the file is present, you read the text from file, else you don’t. But, no runtime error from Python Interpreter.

Conclusion

In this Python Tutorial, we learned how to read file content to a string.