Advertisements
Advertisements
प्रश्न
Write a program to accept a filename from the user and display all the lines from the file which contain python comment character '#'.
कोड लेखन
Advertisements
उत्तर
filename = input("Enter filename: ")
try:
with open(filename, "r") as file:
for line_number, line in enumerate(file, start=1):
if "#" in line:
print(line_number, line.rstrip("\n"))
except FileNotFoundError:
print("File does not exist.")
The program displays both the line number and the matching line.
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
