Computer Science
Write a program that will create an object called filout for writing, associate it with the filename STRS.txt. The code should keep on writing strings to it as long as the user wants.
Python File Handling
2 Likes
Answer
with open('STRS.txt', 'w') as filout:
ans = 'y'
while ans == 'y':
string = input("Enter a string: ")
filout.write(string + "\n")
ans = input("Want to enter more strings?(y/n)...")
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
Related Questions
Write a function in Python to count and display the number of lines starting with alphabet 'A' present in a text file "LINES.TXT". e.g., the file "LINES.TXT" contains the following lines:
A boy is playing there. There is a playground. An aeroplane is in the sky. Alphabets & numbers are allowed in password.
the function should display the output as 3.
Write a program that counts the number of characters up to the first $ in a text file.
Consider the following definition of a dictionary Member, write a method in Python to write the content in a pickled file member.dat.
Member = {'MemberNo.': ..............., 'Name': ...............}
Consider the following definition of dictionary Staff, write a method in python to search and display content in a pickled file staff.dat, where Staffcode key of the dictionary is matching with 'S0105'.
Staff = {'Staffcode': ..............., 'Name' = ...............}