In this tutorial, I will explain how to read the first line of a file in Python. As a developer working on a project, I came across a scenario where I needed to read the first line of a file. After researching various methods I found several effective methods to accomplish this task. Let us learn more about this topic today.
Read the First Line of a File in Python
Let’s say you have a file called customers.txt that contains a list of customers for your business, with one customer name per line:
John Smith
Emily Johnson
Michael Davis
Sarah ThompsonRead How to Save Images to File in Python?
1. Use readline() Method
If you only needed to read the first line of this file (John Smith), you could use the readline() method like this:
with open('customers.txt') as file:
first_line = file.readline()
print(first_line)Output:
John SmithYou can refer to the below screenshot to see the output:

The with statement is used to open the file and automatically close it after reading. The open() function opens the file in read mode by default. Then readline() reads the first line of the file and returns it as a string, which we store in the first_line variable.
Check out How to Import All Functions from a File in Python?
2. Use next() function
Another option is to use the next() function to get the first line:
with open('customers.txt') as file:
first_line = next(file)
print(first_line) Output:
John SmithYou can refer to the below screenshot to see the output:

This also prints out “John Smith”. The next() function returns the next line from the file object each time it’s called.
Read How to Get the Basename of a File in Python?
Store the Remaining Lines in a Python List
If you want to read the first line, but also store the rest of the lines in a list for later use, you could do:
with open('customers.txt') as file:
first_line = file.readline()
rest_of_lines = file.readlines()
print("First line:", first_line)
print("Rest of file:", rest_of_lines)Output:
First line: John Smith
Rest of file: ['Emily Johnson\n', 'Michael Davis\n', 'Sarah Thompson\n']You can refer to the below screenshot to see the output:

Here the readlines() method returns a list containing the remaining lines after the first one was read with readline().
Read How to Get the File Size in MB using Python?
Handle Edge Cases
What if the file is empty or doesn’t exist? You’ll want to add some error handling:
try:
with open('customers.txt') as file:
first_line = file.readline()
print(first_line)
except FileNotFoundError:
print("File not found.")
except IOError:
print("Error reading file.")This will print an error message if the file can’t be found or opened due to permission issues.
Also, keep in mind that readline() the newline character (\n) at the end of the string. To remove it, you can use first_line.rstrip() or first_line[:-1].
Check out How to Import a Class from a File in Python
Conclusion
In this article, I explained how to read the first line of a file in Python. I discussed some important such as using the readline() method, using the next() function, and storing the remaining lines in a Python list. I also discussed how to handle edge cases.
You may like to read:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.