- 要给文件添加内容个,而不是覆盖原有的内容,以附加模式打开文件,Python不会在返回文件对象前清空文件,而写入到文件的行都将添加到文件末尾,如果指定的文件不存在,Python将为你创建一个空文件。
filename = 'programing.txt'
with open(filename,'a') as object:
object.write("I also love finding meaning in large datasets.\n")
object.write("I love creating apps that can run in a brower.\n")
message = "请用户输入姓名:"
h = input(message)
filename = 'guest.txt'
with open(filename,'w') as object:
object.write(h + "\n")
请用户输入姓名:yege
while True:
message = '请输入用户名:'
name = input(message)
if name == 'quit':
break
print(name + '欢迎您!')
filename = 'guest_book.txt'
with open(filename,'a') as object:
object.write(name + '\n')
notes = []
while True:
note = input("你为什么这么喜欢美女?\n")
notes.append(note)
sure = input("是否要继续询问下一个用户?\n")
if sure == '否':
break
filename = 'find.txt'
with open(filename,'a') as a:
for note in notes:
a.write(note + '\n')