file.close() [英] file.close()

查看:116
本文介绍了file.close()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道其他人如何处理文件的关闭。即使我很少看到其他人这样做,我似乎总是以这种模式结束。


f1 = file(''file1'')

试试:

#process f1

终于:

f1.close()


明确关闭f1


或多个文件:


f1 = file(''file1'')

尝试:

f2 =文件(''file2'')

尝试:

#process f1& f2

终于:

f2.close()

终于:

f1.close()


明确关闭f1& f2

打开f1或f2的任何例外都是在这种结构之外处理的,或者是允许从计划中删除
。我知道文件会在进程退出时自动关闭
。我只是好奇其他人如何为这个小脚本和大型程序做这件事。我正在做的是矫枉过正吗?


谢谢,


bryan

i''m curious to know how others handle the closing of files. i seem to
always end up with this pattern even though i rarely see others do it.

f1 = file(''file1'')
try:
# process f1
finally:
f1.close()

which explicitly closes f1

or for multiple files:

f1 = file(''file1'')
try:
f2 = file(''file2'')
try:
# process f1 & f2
finally:
f2.close()
finally:
f1.close()

which explicitly closes f1 & f2
any exceptions opening f1 or f2 is handled outside of this structure or is
allowed to fall out of the program. i''m aware that files will automatically
be closed when the process exits. i''m just curious how others do this for
small scripts and larger programs. is what i''m doing is overkill?

thanks,

bryan

推荐答案

Bryan写道:
Bryan wrote:
明确关闭了f1& f2
打开f1或f2的任何异常都是在此结构之外处理的,或者是否允许不属于该程序。我知道文件将在进程退出时自动关闭。我只是好奇别人怎么做这个
小脚本和大型程序。我正在做的是矫枉过正吗?
which explicitly closes f1 & f2
any exceptions opening f1 or f2 is handled outside of this structure
or is
allowed to fall out of the program. i''m aware that files will
automatically
be closed when the process exits. i''m just curious how others do this
for
small scripts and larger programs. is what i''m doing is overkill?




不,根本不是;它安全便携。 Python语言不支持
指定对象何时被回收,虽然CPython实现了

及时做到了。使用外部资源 - 一旦你完成它们就应该发布

- 最好在try / finally

条款中完成。


-

Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

__美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& & tSftDotIotE

/ \如果没有赢得它的意愿,进入任何战争都是致命的。

\ __ / Douglas MacArthur



No, not at all; it''s safe and portable. Python the language does not
specify when objects get reclaimed, although CPython the implementation
does it promptly. Use of external resources -- which should be released
as soon as you''re done with them -- are best done in try/finally
clauses.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ It is fatal to enter any war without the will to win it.
\__/ Douglas MacArthur


2003年7月23日星期三20:29:27 -0700,Erik Max Francis写道:
On Wed, 23 Jul 2003 20:29:27 -0700, Erik Max Francis wrote:
Bryan写道:
就是我所说的做得过头了?
is what i''m doing is overkill?



外部资源的使用 - 应该在你完成之后立即发布 - 最好在try / finally中完成条款。



Use of external resources -- which should be released as soon as
you''re done with them -- are best done in try/finally clauses.




似乎嵌套''try''子句并不能很好地扩展。如果

打开五十个文件怎么办? ''try''子句的嵌套级别也必须是
50,还要及时关闭它们吗?


-

\上帝禁止任何书籍被禁止。这种做法是因为杀婴而不可原谅。 - Dame Rebecca West |

_o__)|
http: //bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B



It seems that nesting the ''try'' clauses doesn''t scale well. What if
fifty files are opened? Must the nesting level of the ''try'' clauses be
fifty also, to close them promptly?

--
\ "God forbid that any book should be banned. The practice is as |
`\ indefensible as infanticide." -- Dame Rebecca West |
_o__) |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B


Ben Finney写道:
Ben Finney wrote:
似乎嵌套''try''子句并不能很好地扩展。如果打开五十个文件怎么办? ''try''条款的嵌套级别是否还要五十,还要及时关闭它们?
It seems that nesting the ''try'' clauses doesn''t scale well. What if
fifty files are opened? Must the nesting level of the ''try'' clauses
be
fifty also, to close them promptly?




如果你是在一个区块中操纵五十个文件,大概你正在以统一的方式做



allFiles = [...]

尝试:

...

最后:
$ f $ b for allFiles中的每个文件:

eachFile。关闭()


-

Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

__美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& & tSftDotIotE

/ \男人是一个讨厌而不是爱的动物。

\ __ / Rebecca West



If you''re manipulating fifty files in one block, presumably you''re doing
so in a uniform way:

allFiles = [...]
try:
...
finally:
for eachFile in allFiles:
eachFile.close()

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Man is a hating rather than a loving animal.
\__/ Rebecca West


这篇关于file.close()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆