Computer Science

Write a program to display all the records in a file along with line/record number.

Python Data Handling

2 Likes

Answer

Let the file "f1.txt" include the following sample text:

Soft whispers of the wind.
A melody in the trees.
Sun-kissed petals dance.
A garden's quiet song.
file_name = 'f1.txt'
line_number = 1
f = open(file_name, 'r')
for line in f:
    print("Line", line_number, ":", line.strip())
    line_number += 1
f.close()
Output
Line 1 : Soft whispers of the wind.
Line 2 : A melody in the trees.
Line 3 : Sun-kissed petals dance.
Line 4 : A garden's quiet song.

Answered By

2 Likes


Related Questions