Python

Python Read File Into String

Python is a scalable, versatile, and user-friendly language that makes it easy to read files into strings. It provides various methods to read a file and put the content into a string variable. In python, the whole content of a file can be read at once and stored in a string as a whole. In this guide, we will learn the three basic ways to read a text file into the string using the python language, covering different scenarios, with the help of well-explained, detailed, easy, and simple examples. The three common methods to read a file into a string in python are as follow:

  1. read()
  2. readlines()
  3. pathlib()

However, the following steps are common in each method of reading the content from a file:

Step # 1: Open the file in the read mode by using python’s built-in open() function

# variable = open(file path,mode)

Step #2: Read the content in the file by using any method given above, i.e.

# NewVariable = variable.read()
# NewVariable = variable.readlines()
# NewVariable = variable.pathlib()

Step 3: Close the file by using python’s built-in close() function.

# variable.close()

Below you can see the code that opens, read, and closes the file.

The code given above opens the file with the open() function in read ‘r’ mode, reads the text with read() function, and closes the file with the close() function. Let’s see the example of each method and understand how to read files into the string using the three methods mentioned above.

Example 1

The first example explains the read() method. The read() method reads the whole content in the file and returns the text as a single string. The read text is stored in a new variable which is treated as a string. Here is the syntax of read() function:

# file.read([n])

The ‘file’ is the variable that contains the path of the file which is to be read, and ‘[n]’ contains the number of lines that must be read. [n] is optional, which means, if the number is provided, the read() function reads the number of lines specified by [n], and if it is not provided, the read() function will read the whole file. Below is the code to execute the read() function to read the file into a string:

with open(‘text.txt) as s:
text = s.read()
print (text)

Upon executing this code, You will get the following affixed output:

Example 2

The second example explains the readlines() python function to read the file into a string. The readlines() function reads all the lines and returns the list of strings for each line in the file. Below is the code for reading a file into the string using readlines() python function:

with open(‘text.txt’) as s:
text = s.readlines()
print(text)

When you execute this code, you will get the following affixed output:

Example 3

In the third and last example, we will explain the use of pathlib() to read a file into a string. The pathlib() is a module added in python 3.4, containing various classes for file handling. One of the pathlib() module classes is read_text(), an efficient method for file handling and system paths. The pathlib imports the Path library, which provides the read_text() method to read the data from a file. The read_text() opens the file, reads the whole text given in it, and returns it as a string. Here is the code for reading the file into the string using pathlib() classes:

from pathlib import Path
text = Path(“text.txt).read_text()
print (text)

Upon executing this code, the following output will be generated:

Conclusion

In this guide, we have learned the basic concept regarding python reading files into the string format. We have discussed three approaches for reading a file into a string. These methods are read(), readlines(), and pathlib(). There are various other python methods available to read the file into a string. However, the three methods explained above are most commonly used and very easy to learn. It is up to your desire which one of them will suit your work requirement.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content