Python  File  input/output

9.case closed?
below your with … as code,do two things:
1.check if the file is not .closed
2.if that's the case,call .close() on it
3.(you don't need an else here,since your if statement should do nothing if .closed is True)
4.after your if statement,print out the value of my_file.closed to make sure your file is really closed.

with open("text.txt","w") as my_file:
my_file.write("Success!")
if my_file != my_file.closed:
my_file.close()
print my_file.closed

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容