63.7k views
3 votes
Write a program that asks the user for the name of a text file. The program should display the first 10 lines of the file on the screen. If the file has fewer than 10 lines, the entire file should be displayed along with a message indicating the entire file has been displayed.

User Smc
by
5.4k points

1 Answer

5 votes

Answer:

Written in Python

filenm = input("File name: ")

myfile = open(filenm+".txt")

for i in range(10):

line = myfile.readline()

print(line)

print("End of file")

Step-by-step explanation:

This line prompts user for file name

filenm = input("File name: ")

This line creates an instance of the file

myfile = open(filenm+".txt")

This line iterates through the 10 lines

for i in range(10):

This line reads the line

line = myfile.readline()

This line prints each line

print(line)

This line prints a message indicating that all possible lines have been printed

print("End of file")

User Ken Burkhardt
by
5.6k points