Python

Python Read File Into String

File handling is probably one crucial part of programming that a developer must be profound in. File handling includes the actions of creating, opening, reading, updating, deleting, and closing the file. However, let’s just focus on reading the content of the file into a string. To do that, the user can utilize the read(), readlines(), and pathlib() methods.

Let’s start with a demonstration of the first method

Method 1: Using read() to Read File Into String

The read() method is used to read all of the content of the file into a variable. When the variable is checked with the type() method, the type is returned as a string. Meaning that the data has been read into a string. To verify this, let’s create a file with the following content within the same location as the python program:

In the python code, start by opening up the file with the open() method in the “r” mode:

fileVar = open("readMe.txt","r")

 

After that, read the data of the file using the read() method and store it inside a variable “data”:

data = fileVar.read()

 

After that, print out the “data” variable and also the type of this variable using the type() method:

print(data)
print(type(data))

 

When this code is executed, it produces the following output on the terminal:

It can be observed from the output that the data was read as a string.

Method 2: Using readlines() to Read File Into String

The readlines() method is used to read multiple-line content from a file into a list, where each element in the list represents an individual line. However, to convert it into a string instead of a list, the user can utilize the for-in loop. To demonstrate this, take the following content in the file:

For the Python code, take the following lines:

fileVar = open("readMe.txt","r")

data = fileVar.readlines()

print("The initial data is: ",data)
print(type(data))

 

In this code snippet, the file is opened and read using the readlines() method and then the content of the “data” variable is printed onto the terminal along with the type:

The data is read in the form of a list, to convert it into a string, add in the following code snippet:

finalVar = ""
for item in data:
    finalVar += item

print(finalVar)

 

In this code snippet, an empty string is created and every item of the list is added to the string using the string concatenation “+”. When this code is executed, it produces the following result in the terminal:

From the output, it can be seen that the file’s content has been read and stored in a string.

Method 3: Using the pathlib to Read File Into String

The Path package from the pathlib library can be used to create a Path object in Python which contains a function to read the text of a file defined by the Path. The method is called the read_text() and to demonstrate its working, simply take the same file in method 2 and use the following code snippet:

from pathlib import Path

file = Path("readMe.txt")

data = file.read_text()

print(type(data))
print(data)

 

In this code snippet:

  • A new Path object is created using its Path() constructor and the relative path of the file to be read is passed inside the arguments.
  • The read_text() method is applied on the Path object and the result is stored inside the “data” variable
  • Lastly, the data is printed out on the terminal along with its type.

When this program is executed, it produces the following result on the terminal:

The output verifies that the data has been read into a string variable successfully.

Conclusion

The user can easily read the content of a file in Python and store it in a string using the read() method, the readlines() method with string concatenation, and the Path object with the read_text() method. There is not much difference in the working of these methods. All of these methods have been demonstrated in this post.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.