python关闭文件-为什么要用Python关闭文件?

(1) It is a matter of good programming practice. If you don"t close

them yourself, Python will eventually close them for you. In some

versions of Python, that might be the instant they are no longer

being used; in others, it might not happen for a long time. Under

some circumstances, it might not happen at all.

(2) When writing to a file, the data may not be written to disk until

the file is closed. When you say "output.write(...)", the data is

often cached in memory and doesn"t hit the hard drive until the file

is closed. The longer you keep the file open, the greater the

chance that you will lose data.

(3) Since your operating system has strict limits on how many file

handles can be kept open at any one instant, it is best to get into

the habit of closing them when they aren"t needed and not wait for

"maid service" to clean up after you.

(4) Also, some operating systems (Windows, in particular) treat open

files as locked and private. While you have a file open, no other

program can also open it, even just to read the data. This spoils

backup programs, anti-virus scanners, etc.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值