मराठी

File = open('textfile.txt','w') word = '' while word.upper() != 'END': word = raw_input('Enter a word use END to quit') file.write(word + '\n') file.close Using the file mentioned above,

Advertisements
Advertisements

प्रश्न

file = open('textfile.txt','w')
word = ''
while word.upper() != 'END':
     word = raw_input('Enter a word use END to quit')
     file.write(word + '\n')
file.close

Using the file mentioned above, write a function to read the file and display numbered list of words.

कोड लेखन
Advertisements

उत्तर

def display_words():
    with open("textfile.txt", "r") as file:
        number = 1
        for word in file:
            print(number, word.rstrip("\n"))
            number += 1

 display_words()
 

Each line is read sequentially and displayed with its serial number.

shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 7: Data File Handling - EXERCISE [पृष्ठ १४५]

APPEARS IN

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

Englishहिंदीमराठी


      Forgot password?
Use app×