Closing an open file automatically on quit?
如何使之在播放器尝试退出时,python自动关闭打开的文件?
这就是我目前为止所拥有的:
1 2 3 4 5
| hai = open('hai.txt','a')
quit = 0
while quit == 0:
hai.write('SPAM')
hai.close() |
- 在大多数情况下,即使不使用"with"语句,python也会为您接受它。blog.lerner.co.il/dont-use-python-close-files-answer-depends
使用 </P >
1 2
| with open("file.txt","a") as f:
#Do things with f |