| by Arround The Web | No comments

How to use Python readline() function

The “readline()” function in Python is a powerful tool for reading lines of text from a file or other input source. It is a built-in function within the `io` module that allows for efficient and flexible handling of large files and complex data structures.

This article aims to delve into the intricacies of the readline() function, highlighting its various features and functionalities, and demonstrating its utility in real-world scenarios.

What is the “readline()” Function?

At its core, the readline() function reads a single line of text from a given input stream, such as a file, and returns it as a string. The syntax for the readline() function is:

file."readline()"(size)

The “size” parameter is optional. It specifies how many bytes from the line should be returned. If no size is specified or if it is set to -1, the function will read the entire line.

Return Value

The line read from the file is returned by this procedure. Only when an immediate EOF is encountered is an empty string returned.

How to Use the “readline()” Function?

To use the readline() function, you need to perform these steps.

Step 1: One must first open the desired file using Python’s file-handling operations. It can be done by:

my_file = open(file.txt, “r”)

The file’s name is passed as the first parameter to the “open()” method, and the mode you want to open in is passed as the second parameter. The letter “r” indicates that the file will open in read-only mode.

Step 2: Once the file is open, the readline() function can be called to read each line sequentially.

line = my_file.readline()

The read line is saved in “line”.

Step 3: To print the lines:

print(line)

Step 4: Once the file is read, all you have to do now is close it.

my_file.close()

Here is the full code to use the readline() function to read lines from a file:

my_file = open("D:\linux.txt", "r")

line = my_file.readline()

print(line)

my_file.close()

Output

How to Use Python readline() Function with Size Argument?

The readline() function takes an optional argument, “size”, which specifies the maximum number of characters to be read. This enables users to capture specific portions of a file or select substrings of interest, enhancing the flexibility and efficiency of text processing tasks.

To read a file with a specified size:

my_file = open("D:\linux.txt", "r")

line = my_file.readline(6)

print(line)

my_file.close()

The above code opens the “linux.txt” file in read-only mode. Using “my_file.readline(6)”, it then reads the first six characters from the file, assigning the outcome to the variable “line”. Then, it prints the contents of the line, which are the file’s first six characters. The code then uses “my_file.close()” to close the file to free up system resources related to the file.

Output

How to Use Python readline() Function to Read a File Line by Line?

By iterating over a file object using a loop, we can also read a file line by line.

my_file = open("D:\linux.txt", "r")

line = my_file. readline()

while line:

        print(line)

        line = my_file."readline()"()

my_file.close()

This Python code opens the “linux.txt” text file in read mode. After that, it uses the “readline()” method to read the file’s first line and assigns it to the variable “line.” To read more lines from the file, the program enters a while loop, which runs indefinitely.

Using the print() function, each line is output to the console while the loop is running. After printing a line, “readline()” is used to read the following line, and so on until there are no more lines left to read. “my_file.close()” is used to close the file and release system resources.

Output

Conclusion

The readline() function in Python is a powerful yet flexible tool for reading lines of text from different input sources. It can be used to read a line from a file, read a line of a specific size from a file, and read all lines from a file line-by-line.

Share Button

Source: linuxhint.com

Leave a Reply