Python Program to Read First n Lines of a File

Files in Python:

Files are identified locations on the disc where associated data is stored. They’re used to keep data in non-volatile memory for a long time (e.g. hard disk).

We use files for future usage of the data by permanently saving it because Random Access Memory (RAM) is volatile (it loses its contents when the machine is turned off).

We must first open a file before we can read from or write to it. When we’re finished, it needs to be closed so that the file’s resources may be released.

As a result, a file operation in Python is performed in the following order:

  • Create a new file
  • You can either read or write (perform the operations)
  • Close the file.

Given a file, the task is to read the first n lines of the given file in python

Program to Read First n Lines of a File in Python

Below is the full approach for reading the first n lines of the given file in python

Method #1: Using For loop (Static Input)

Approach:

  • Import islice function from the itertools module using the import keyword.
  • 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.
  • Give the n value as static input and store it in a variable.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Pass the given file and given n value as arguments to the islice() function and iterate in that using the for loop.
  • Inside the for loop, Print the iterator value i.e given n lines in a file.
  • The Exit of Program.

Below is the implementation:

# Import islice function from itertools module using the import keyword
from itertools import islice
# 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"
# Give the n value as static input and store it in a variable.
gvn_n_val = 3
# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
  # Pass the given file and given n value as arguments to the islice() function 
  # and iterate in that using the for loop.
  for f_line in islice(givenfilecontent, gvn_n_val):
    # Print the iterator value i.e given n lines in a file 
    print(f_line)

Output:

hello this is btechgeeks 
welcome to btechgeeks 
Good morning this is btechgeeks

Method #2: Using For loop (User Input)

Approach:

  • Import islice function from itertools module using the import keyword.
  • 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.
  • Give the n value as user input using the int(input()) function and store it in a variable.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Pass the given file and given n value as arguments to the islice() function and iterate in that using the for loop.
  • Inside the for loop, Print the iterator value i.e given n lines in a file.
  • The Exit of Program.

Below is the implementation:

# Import islice function from itertools module using the import keyword
from itertools import islice
# 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"
# Give the n value user input using the int(input()) function and store it in a variable.
gvn_n_val = int(input("Enter some random number = "))
# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
  # Pass the given file and given n value as arguments to the islice() function 
  # and iterate in that using the for loop.
  for f_line in islice(givenfilecontent, gvn_n_val):
    # Print the iterator value i.e given n lines in a file 
    print(f_line)

Output:

Enter some random number = 4 
hello this is btechgeeks 
welcome to btechgeeks 
Good morning this is btechgeeks 
By now you might be aware that Python is a Popular Programming Language

File Content:

hello this is btechgeeks
welcome to btechgeeks
Good morning this is btechgeeks 
By now you might be aware that Python is a Popular Programming Language 
used right from web developers to data scientists. Wondering what exactly Python looks like and how it works? 
The best way to learn the language is by practicing. BTech Geeks have listed a wide collection
of Python Programming Examples.

Google Colab Images:

Files and Code: