Python File readline() Method with Examples

How to Use the File readline() Method in Python

In this tutorial, you'll learn how to use the file readline() method in Python. The file readline() method reads and returns a single line from the file.

Python File readline() Syntax

The syntax of the file readline() method is as follows:

file.readline()

Remarks

  • The readline() method reads a single line from the file.
  • A newline character (\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline.
  • If you want to read all the lines of a file and convert them to a list, then use the readlines() method.

Examples

The following is the demo file named employee_list.txt file containing the following text:

1- ID: 001 | Name: Employee 001 | Department: Sales & Marketing
2- ID: 002 | Name: Employee 002 | Department: IT
3- ID: 003 | Name: Employee 003 | Department: Business Development
4- ID: 004 | Name: Employee 004 | Department: Finance & Accounting
5- ID: 005 | Name: Employee 005 | Department: IT
6- ID: 005 | Name: Employee 005 | Department: Finance & Accounting

Example 1: Read the First and Second Lines

The following code reads the 1st and 2nd lines of the file employee_list.txt:

employee_list_file = open("employee_list.txt", "r")
first_line = employee_list_file.readline()
second_line = employee_list_file.readline()
print (first_line)
print (second_line)
employee_list_file.close()

Output:

ID: 001 | Name: Employee 001 | Department: Sales & Marketing

ID: 002 | Name: Employee 002 | Department: IT

Example 2: Read the Top 4 Lines

The following code reads the top 4 lines of the file employee_list.txt, using the while loop and break statements:

employee_list_file = open("employee_list.txt", "r")
count = 0
while(True):
count += 1
print(employee_list_file.readline(), end="")
if count == 4:
break
employee_list_file.close()

Output:

1- ID: 001 | Name: Employee 001 | Department: Sales & Marketing
2- ID: 002 | Name: Employee 002 | Department: IT
3- ID: 003 | Name: Employee 003 | Department: Business Development
4- ID: 004 | Name: Employee 004 | Department: Finance & Accounting

Example 3: Read all Lines

The following sample code illustrates an example of how to loop through the file object using the for loop statement and read all lines from the file:

employee_list_file = open("employee_list.txt", "r")
for line in employee_list_file:
print (line, end='')
employee_list_file.close()

Output:

1- ID: 001 | Name: Employee 001 | Department: Sales & Marketing
2- ID: 002 | Name: Employee 002 | Department: IT
3- ID: 003 | Name: Employee 003 | Department: Business Development
4- ID: 004 | Name: Employee 004 | Department: Finance & Accounting
5- ID: 005 | Name: Employee 005 | Department: IT
6- ID: 006 | Name: Employee 006 | Department: Finance & Accounting

Example 4: Read and Convert a List of Items

The following code reads all the lines of a file named employee_list.txt and converts them to a list:

employee_list_file = open("employee_list.txt", "r")
lines = employee_list_file.readlines()
print(lines)
employee_list_file.close()

Output:

['1- ID: 001 | Name: Employee 001 | Department: Sales & Marketing\n', '2- ID: 002 | Name: Employee 002 | Department: IT\n', '3- ID: 003 | Name: Employee 003 | Department: Business Development\n', '4- ID: 004 | Name: Employee 004 | Department: Finance & Accounting\n', '5- ID: 005 | Name: Employee 005 | Department: IT\n', '6- ID: 006 | Name: Employee 006 | Department: Finance & Accounting']

In this tutorial, you've learned how to use the Python file readline() method. The file readline() method is a function to read and return a single line from a file.


See also:
Python return Statement with Examples
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python

Leave a Comment