Reading First line in Python - How can you read only the first line from a file in Python?
Sometimes you are interested in reading only the first line of a text in your Python program due to some specific project requirement. Python provides many ways to read only the first line of a file efficiently. In this tutorial I will share with you Python code and teach you by running the Python program to read the first line of a file.
Read only the first line in Python -Why should you read only the first line of a file in Python?
There are many situations where you just have to read the first line of a text file in a Python program. Now we are trying to list down the cases where you just have to read the first line of the text file. Here are the possible scenarios:
For Getting File Header Information
In order to understand the CSV file it is necessary to read the first line of the file to find out columns in the dataset. Sometimes you want to understand the metadata of the given file, then you can read the first line of your file.
Data Validation
If you are going to process the data sent to you then by reading and analyzing the first line of file you can perform the first step of your data validation. So, to validate the data you will have to read the first line of your data file.
Program Performance
You can read the first line of the file and understand the data and validate it against your business logic. Reading one line is much easier instead of reading the whole file and then performing the validation. Reading one line also consumes very less memory and your program performs well.
Log File Analysis
If you are going to analyze the log file then by just reading the first line of the file you will be able to know the log file format. After knowing the file format you can start applying specific data formatting and analysis logic.
There could be many other use cases where you can read the first line of file to meet your specific project needs.
Here is the video tutorial of reading only first line from the file.
YOUTUBE_VIDEO_1
Python code examples of reading only first line of a file
Now lets look at the examples of reading only first line of a file in Python program.
1. Use the .readline() method
The readline() function is used to read one complete line from the file and return the line by adding a new line character ?\n?. You have to manually remove the new line character before using the line in data processing. Here is the complete example of the readline() function which returns the first line from the file.
Example code of reading first line of text file using .readline() method:
# Use the .readline() method
with open('data.txt','r') as f:
first_line = f.readline()
print("**",first_line,"**")
Here is the screenshot of the above program output:
2. Removing the new line character from the line
Before using the line in the program we should remove the new line character '\n' using the function string('\n'). Here is the complete example of removing new line character from the line:
# Removing the new line character from the line
with open('data.txt','r') as f:
first_line = f.readline().strip('\n')
print(first_line)
Here is the output of the above example code:
You can see in the above screen shot line data is printed after removing the new line character '\n'.
3. By creating file object
You can create the reference of a file with the help of open() function and then use the readline() function in another line. Here is complete code:
# By creating file object
f = open('data.txt','r')
first_line = f.readline().strip('\n')
print(first_line)
f.close()
Here is the output of the above program:
In the above code you should close the file object by calling f.close() method.
4. In one line code
You can also read the first line of a file in just one line. Here is the complete code of reading first line of file in one line of code:
# In one line code
file_first_line = open('data.txt','r').readline().strip('\n')
print(file_first_line)
Here is the screen shot of the Python code in reading a line from text file in Python:
In the above screenshot you can see the above code prints the content in the first line.
5. Go to beginning of file and read
You can open a file using the open() function and then use the seek(0) method before reading the file. The seek(0) moves the cursor to the beginning of the file. Here is complete come example:
# Go to beginning of file and read
f = open('data.txt','r')
f.seek(0)
first_line = f.readline().strip('\n')
print(first_line)
f.close()
Here is the output of using seek() method in Python:
Above code shows the use of show() function while reading the first line of a file.
06. Using linecache library
We are going to use the linecache Python module for reading the first line of the file. The linecache module comes as a Python standard library that can be used with the text (or source code) files. This module implements the cache that holds the contents of files, parsed into separate lines in the memory. Here is the complete example of linecache library for reading first line of file:
# Using linecache
import linecache
# Read fist line
print(linecache.getline('data.txt',1))
Here is the output of using linecahce example of reading first line in Python:
1Above screenshot shows the use and output of program which uses linecache.getline() function to read first line from the file.
In this section we learned to use various methods in Python for opening a file and then reading the first line from the file. Python is a powerful language for handling files in the Python programming language. In this section we touched upon the examples of reading only the first line from the file in programming language.
Related Python Tutorials
2