How to Read First Line of File in Python

Python provides many functions that you can use to handle file operations. Reading a file is one of the operations. It is one of the common tasks in programming. When the file is larger – let’s say more than a GB then the best way to read the file in chunks at a time. Let’s see different ways to read the first line of a file.

Python get first line of file

To read the first line of a file in Python, use the file.readline() function. The readline() is a built-in function that returns one line from the file. Open a file using open(filename, mode) as a file with mode “r” and call readline() function on that file object to get the first line of the file.

Syntax of readline()

file.readline(size)

Arguments

The size is an optional argument that indicates several bytes from the line to return.

Python code to get the first line of a file

with open("app.txt", "r") as file:
 first_line = file.readline()

print(first_line)

Output

This is the first line of the file.

Explanation

We need a text file that we can read in our Python program. To do that, I created an app.txt file whose content is the following.

This is the first line of the file.
This is the second line of the file.
This is the third line of the file.
This is the last line of the file.

Now, to open a file in Python, we use the with open() function and pass the filename and mode, app.txt, and r. Then we used a file.readline() function to get the content of the first line and save it in the first_line variable. Then, finally, we print the content of the first line.

Using read() function to read the first line

The combination of read() and split() functions can give you the first line of the file in Python. The read() is a built-in Python function that helps you read the file’s content.

The split() function is helpful to split the content of the file. So we split the file after the first line, and hence we get the first line of the file. The split() function lists all the lines separated based on the newline character and extracted the first line from this list.

with open("app.txt", "r") as file:
 content = file.read()
 first_line = content.split('\n', 1)[0]

print(first_line)

Output

This is the first line of the file.

In this example, as usual, to open a file, we used a with open() statement, and then to read a file, we used a read() function and then used the split() function to split the lines and get the first line using indexing.

Applying readlines() function to get the first line

The readlines() is a built-in Python function that reads all the lines from the file and returns a list of each line as the list element and extracts the first line from the returned list.

with open("app.txt", "r") as file:
 first_line = file.readlines()[0].rstrip()

print(first_line)

Output

This is the first line of the file.

In this example, we use the combination of readlines() function and indexing to get the first line and then use a rstrip() function to eliminate any empty spaces after the first line.

Using next() function to read first line

The next() is a built-in Python function that returns the next element in an iterator. So if we pass the file object to the next() function, it returns the first line of the file.

with open("app.txt", "r") as file:
 first_line = next(file).rstrip() 

print(first_line)

Output

This is the first line of the file.

In this example, we treated with open() as an iterator object and using the next() method, we got the first line of the file, and we printed the line using the print() function.

That’s it for this tutorial.

Leave a Comment