KnowledgeBoat Logo

Class - 12 CBSE Computer Science Important File Handling Questions 2025

Write a code snippet that will create an object called fileout for writing; associate it with the filename 'STRS'. The code should keep on writing strings to it as long as the user wants.

Python Data Handling

1 Like

Answer

fileout = open('STRS.txt', 'w')
ans = 'y'
while ans == 'y':
    string = input("Enter a string: ")
    fileout.write(string + "\n")  
    ans = input("Want to enter more strings?(y/n)...")   
fileout.close()
Output
Enter a string: Hello
Want to enter more strings?(y/n)...y
Enter a string: World!
Want to enter more strings?(y/n)...n

The file "STRS.txt" includes:

Hello
World!

Answered By

2 Likes