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
Step #2: Read the content in the file by using any method given above, i.e.
# NewVariable = variable.readlines()
# NewVariable = variable.pathlib()
Step 3: Close the file by using python’s built-in close() function.
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.
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:
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:
text = s.read()
Upon executing this code, You will get the following affixed output:
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:
text = s.readlines()
When you execute this code, you will get the following affixed output:
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:
text = Path(“text.txt”).read_text()
Upon executing this code, the following output will be generated:
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.