从键盘输入若干字符串,逐个将他们写入文件data.txt的尾部,直到输入“*”时结束,然后从该文件中逐个读出字符串,并在屏幕上显示出来
# 将用户输入写入文件
with open('data.txt', 'a+', encoding='utf-8') as fp:
while 1:
c = input('输入任意字符串:')
if c.strip() == "*":
break
fp.write(c)
# 读文件
with open('data.txt', 'r+', encoding='utf-8') as fp:
print(fp.read())
不知道你这个问题是否已经解决, 如果还没有解决的话:有帮助的话,请点采纳该答案~