हिंदी

Write a program to accept a filename from the user and display all the lines from the file which contain python comment character '#'.

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 7: Data File Handling - EXERCISE [पृष्ठ १४६]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
अध्याय 7 Data File Handling
EXERCISE | Q 20. | पृष्ठ १४६
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×