Computer Science
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
3 Likes
Related Questions
When a file is opened for output in write mode, what happens when
(i) the mentioned file does not exist
(ii) the mentioned file does exist ?
What role is played by file modes in file operations ? Describe the various file mode constants and their meanings.
How many file objects would you need to create to manage the following situations ? Explain.
(i) to process three files sequentially
(ii) to merge two sorted files into a third file.
Write the advantages of saving data in:
(a) Binary form
(b) Text form