Exercise on python file handling

Write a Python program to read the first n lines of a file using functions.

def read_first_n_lines(filename, n):
with open(filename, ‘r’) as file:
for i in range(n):
line = file.readline()
if not line:
break
print(line.strip())

filename = ‘your_file.txt’
n = 5
read_first_n_lines(filename, n)

1 Like

Very good @pavanmanish731. keep practicing and active