Write a program that asks the user for the name of a file.
The program should display only the first five lines of the file’s contents.
If the file contains less than five lines, it should display the file’s entire contents. by python using while loop
PYTHON CODE :
file = open("input.txt","r") # open the file
sentence = file.readlines()
i = 0
for ln in sentence: # reading line by line
if i < 5:
print((i + 1), ln.strip())
i = i + 1
INPUT :
OUTPUT :
Get Answers For Free
Most questions answered within 1 hours.