In: Computer Science
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Please find the code below and execution screenshot, i have added all the required comments at each line explainning what does it done. So please go through the python code
#the following below program will read the file that has provided as the file name
#reading the file name from input
input_file_name=str(input("Please input the name of the file that needs to be read and display line by line:"))
#opening the file with the given name in read mode
read_file=open(input_file_name,'r')
#reading the line into a line variable
line_variable=read_file.readline()
#iterating to the loop untill the end of the page and all lines read
while(line_variable!=""):
#printing the read line by removing the extra \n as print will display in new line at every time. If you want extra line space you can remove that .strip("\n")
print(line_variable.strip("\n"))
#reading the another line
line_variable=read_file.readline()
#closing the file
read_file.close()
Screenshot of the execution: i have used the name of the file as test
test file contains: