退出时自动关闭打开的文件?

时间:2015-09-01 19:28:26

标签: python python-3.x

如何在播放器尝试退出时自动关闭打开的文件?

这是我到目前为止所做的:

hai = open('hai.txt','a')
quit = 0
while quit == 0:
    hai.write('SPAM')
hai.close()

1 个答案:

答案 0 :(得分:1)

使用

with open("file.txt","a") as f:
    #Do things with f