Class - 12 CBSE Computer Science Important File Handling Questions 2025
Write a program to accept a filename from the user and display all the lines from the file which contain Python comment character '#'.
Python Data Handling
3 Likes
Answer
Let the file "notes.txt" include the following sample text:
Welcome to the Garden of Dreams
#where the ordinary becomes extraordinary
#the impossible becomes possible.
file_name = input("Enter the filename: ")
file = open(file_name, 'r')
lines = file.readlines()
for line in lines:
if '#' in line:
print(line)
file.close()
Output
Enter the filename: notes.txt
#where the ordinary becomes extraordinary
#the impossible becomes possible.
Answered By
1 Like