| by Arround The Web | No comments

Python readlines()

In Python, files are often necessary for data processing since they contain data. Reading files allows us to access and manipulate stored data, such as text documents, CSV files, and JSON files. Reading files also helps us to visualize data, detect patterns, and understand the data structure in a better manner. If you’re working with text files in Python, you might have come across the “readlines()” method.

This Python blog will explain the “readlines()” method in Python using appropriate examples.

What is the “readlines()” Method in Python?

The “readlines()” method is a built-in functionality in Python that reads all the lines from a text file and returns them as a list. It is such that each line in the file is a separate element in the list.

Syntax of “readlines()” Method

file.readlines(sizehint)

In the above syntax, “file” is the name of the file that you want to read. The “sizehint” parameter is optional and specifies the max number of bytes to be read.

“readlines()” Method Parameters

The “sizehint” parameter in the “readlines()” method can have the following values:

  • If “sizehint” is not specified or is set to “-1”, the entire file is read and returned as a list.
  • If “sizehint” is specified as a positive integer, only that many bytes are read from the file.
  • If “sizehint” is specified as “0”, an empty string is returned.

The original text file containing multiple lines has been shown in the below snippet:

Note: This file will be dealt with throughout the write-up.

Example 1: Read All the Lines From the Specified File

The below code is used to read all the lines from the given particular file:

with open("filename.txt", "r") as file:
    lines = file.readlines()
    for line in lines:
        print(line)

In the above code:

  • The file named “txt” is opened in “read” mode and assigned to the object named “file”.
  • After that, the “readlines()” method is used to read all the lines from the particular file.
  • Lastly, loop through the list of lines using the “for” loop and print each line on the console using the “print()” function.

Output

All the lines from the specified text file have been displayed in the above snippet.

Example 2: Read Particular Lines From the Input File

The following code is used to read the particular lines from the given text file:

with open("filename.txt", "r") as file:
    lines = file.readlines(20)
    for line in lines:
        print(line)

According to the above code:

  • The “readlines()” method is used to read the first “20” bytes of the file named “txt“.
  • The “for” loop is used to loop through the list of lines and print each line to the console.

Output

The above output shows that the specified bytes from the given file have been read successfully.

Example 3: Returning List of Lines

The below code snippet is used to read all the lines from the specified file and return them as a list:

with open("filename.txt", "r") as file:
    lines = file.readlines(0)
    print(lines)

In the above code block:

  • The file is opened in read mode using the “open()” method.
  • The “readlines()” method is used to read every line from the particular file named “txt“.
  • Lastly, the list containing the read multiple lines is displayed to the output/console via the “print()” function.

Output

In the above snippet, it can be implied that multiple lines have been read and returned in the form of a list.

Conclusion

The “readlines()” method in Python is utilized to read all lines from a text file and retrieve them as a list. We have also looked at some examples of using the “readlines()” method with different parameter values such as reading specific bytes from the given file or the whole file. This blog demonstrated the working of the “readlines()” method in Python.

Share Button

Source: linuxhint.com

Leave a Reply