Python

Python readlines()

Python is quite an easy and simplest language to start learning. It allows you to read, write and display the data from different files, i.e. text, word, or CSV’s. Python came up with the function readlines() to read the data from some specific file up to the end and display any Python tool on the output console. Therefore, we will be looking at the working of python’s readlines() function within our article today. Ensure Spyder 3 is installed and configured on your Windows 10 operating system. Let’s get started with some examples.

Example 01:

Let’s get started with the simplest example to read the file lines within Spyder 3. Assume that you have a simple text file named “file.txt” residing in your “.spyder-py3” folder of Windows 10 with some three-line text data in it.

To display this data on your Spyder-3 console, you need to open this file in Spyder3 via the python code and read its data afterwards. To open this file, you need to create a file descriptor “f” and call the open() function with it to open the file “file.txt” in a read mode via ‘r’. Now, the file has been opened and can be read using the file descriptor “f”. You have to call the python function “readlines()” with descriptor “f” to get all the lines until the end of the file. These lines will be saved to another variable, “L”. On the third line, we have utilized the print function to display all the lines of the file.txt file in a single row with the variable “L”.

f = open('file.txt', 'r')
L = f.readlines()
print(L)

Save this code, debug and run it with the “debugging” button of Spyder 3. In return, we have got all the data of file “file.txt” displayed on the Spyder 3 console as shown. You can see that the data has not been displayed in the form of lines but in a single row with \n symbol indicating a line break here. Each line has been displayed within ‘’ commas.

Example 02:

The code executed in the first example displayed the data of a file in a single line, even using the readlines() function. But, we want to display the data in separate lines as displayed in the file.txt file itself. For this, you need to utilize the “for” loop in your python code. So, we have updated the above code in Spyder 3, as shown below. After opening the file “file.txt” in a read mode with the open() function of python, we have saved the file object in the file descriptor “f”. Now, the same readline() function has been called with the file descriptor “f” to save the data within the variable “L”. Now, the “for” loop came up here for our help to read each line “l” from the variable “L” and display it on the Spyder3 console using the “print()” function. The code is ready to be used for debugging and running.

f = open('file.txt', 'r')
L = f.readlines()
for l in L:
    print(l)

After running this code with the “run” button of Spyder3, we have got the below-shown result. You can see that the variable “L” data has been displayed on the console in separate three lines as it was in the file.txt text file itself.

Example 03:

From the above example output, you can see that we have got a break of 1 line after every line. While the file.txt file doesn’t contain a whole space of 1 line in it. We need to update the same code to remove that one-line space. So, after the file opening of a file, we have read its data through the “readlines() function the same way. The “for” loop has been updated as the print statement has been using the strip() function to cut off the 1 line break using the “l” iterator. Also, the format() function has been used to define the line style of file.txt data.

f = open('file.txt', 'r')
L = f.readlines()
for l in L:
    print("{}".format(l.strip()))

After running this updated code within Spyder 3, we have got all the lines of the file.txt file in the same manner as it was in the file itself, i.e. without a line break.

Example 04:

Here comes the last but not the least example of using the readlines() function in Python code. The overall code is the same as the upper codes. The only change has been done on the first line by using the “with” keyword to open the file with the file descriptor “f”.

with open("file.txt") as f:
    L = f.readlines()
    for l in L:
        print("{}".format(l.strip()))

After running this code, we got the same output as in the above example.

Conclusion:

To sum up, we have covered all the necessary examples and methods to display the working of Python’s readlines() function. We have started with a very simple example to display the lines of a text file in a single row. After that, we have come up with the display of lines separately through the strip() function.

About the author

Kalsoom Bibi

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