Python readlines() – Python File readlines() Method with Examples

Files in Python:

Python readlines(): Python file handling is a way of saving program output to a file or reading data from a file. File handling is a crucial concept in the programming world. File management is used in almost every form of project. Assume you are constructing an inventory management system. You have data in the inventory management system related to sales and purchases, thus you must save that data somewhere. Using Python’s file management, you can save that data to a file. You must be given data in the form of a comma-separated file or a Microsoft Excel file if you wish to conduct data analysis. Using file handling, you can read data from a file and write output back into it.

File readlines() Method in Python:

f.readlines(): The readlines() method returns a list with each line in the file represented as a list item.

To limit the number of lines returned, use the length parameter. No additional lines are returned if the total number of bytes returned exceeds the specified number.

Syntax:

file.readlines(length)

Parameters

length: This is optional. No additional lines will be returned if the number of bytes returned exceeds the length number. The default value is -1, which means that all lines are returned.

Return Value:

This method’s return type is <class ‘list’>, and it returns the lines as a list.

File readlines() Method with Examples in Python

Example1

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read mode. In this case, we’re simply reading the contents of the file.
  • Print all the lines of the file using the readlines() function.
  • Close the given file using the close function.
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in read mode. In this case, we're simply reading the contents of the file.
gvn_file = open(givenFilename, 'r') 
# Print all the lines of the file using the readlines() function.
print(gvn_file.readlines())
# Close the given file using the close function
gvn_file.close()

Output:

['Hello this is btechgeeks\n', 'Good morning btechgeeks\n', 'welcome to python-coding platform']

Example2: Passing some random number as an argument

Readlines() python: If the total number of bytes returned is greater than given number, do not return the next line.

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read mode. In this case, we’re simply reading the contents of the file.
  • Pass some random number(length) to the readlines() function to get the given number of bytes from the file and print the result.
  • Close the given file using the close function.
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in read mode. In this case, we're simply reading the contents of the file.
gvn_file = open(givenFilename, 'r') 
# Pass some random number(length) to the readlines() function to 
# get the given number of bytes from the file and print the result.
# If the total number of bytes returned is greater than given number, do not return the next line.
print(gvn_file.readlines(25))
# Close the given file using the close function
gvn_file.close()

Output:

['Hello this is btechgeeks\n', 'Good morning btechgeeks\n']

File Content:

Hello this is btechgeeks
Good morning btechgeeks
welcome to python-coding platform

Google Colab Images:

Files and Code: