打开文件而不关闭它们 [英] Opening files without closing them

查看:76
本文介绍了打开文件而不关闭它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在阅读一些python代码,我看到了类似

这样的东西:


contents = open(file).read()


当然你也可以这样做:


open(文件,w)。write(obj)


为什么他们没有关闭文件?这个草率编程还是

文件在引用被销毁时自动关闭(在这个

行之后)?我通常使用:


试试:

f = open(文件)

contents = f.read()
最后


f.close()


但现在我想知道这是否是同样的事情。你宁愿使用哪种方法?b $ b?为什么?


谢谢,

Sandra

I was reading over some python code recently, and I saw something like
this:

contents = open(file).read()

And of course you can also do:

open(file, "w").write(obj)

Why do they no close the files? Is this sloppy programming or is the
file automatically closed when the reference is destroyed (after this
line)? I usually use:

try:
f = open(file)
contents = f.read()
finally:
f.close()

But now I am wondering if that is the same thing. Which method would
you rather use? Why?

Thanks,
Sandra

推荐答案

Sandra-24写道:
Sandra-24 wrote:
我最近正在阅读一些python代码,我看到了这样的内容:

contents = open(file).read( )

当然你也可以这样做:

打开(文件,w)。写(obj)

为什么他们这样做没有关闭文件?这个草率的编程还是
文件在引用被销毁时自动关闭(在此
行之后)?我通常使用:

尝试:
f =打开(文件)
contents = f.read()
最后:
f.close()
I was reading over some python code recently, and I saw something like
this:

contents = open(file).read()

And of course you can also do:

open(file, "w").write(obj)

Why do they no close the files? Is this sloppy programming or is the
file automatically closed when the reference is destroyed (after this
line)? I usually use:

try:
f = open(file)
contents = f.read()
finally:
f.close()




以上相当于:


open(file){| f |

contents = f.read

}


处理所有内容的逻辑封装在open。


但是可以用更少的红宝石方式完成:)


lopex



this above is equivalent to:

open(file){|f|
contents=f.read
}

the logic taking care of everything is encapsulated in open.

but can be done in less ruby way way :)

lopex


Marcin Miel ?? y ?? ski写道:
Marcin Miel??y??ski wrote:
Sandra-24写道:
Sandra-24 wrote:
我最近正在阅读一些python代码,我看到了这样的内容:

contents = open (文件).read()

当然你也可以这样做:

open(文件,w)。write(obj)

为什么他们没有关闭文件?这个草率的编程还是
文件在引用被销毁时自动关闭(在此
行之后)?我通常使用:

尝试:
f =打开(文件)
contents = f.read()
最后:
f.close()
I was reading over some python code recently, and I saw something like
this:

contents = open(file).read()

And of course you can also do:

open(file, "w").write(obj)

Why do they no close the files? Is this sloppy programming or is the
file automatically closed when the reference is destroyed (after this
line)? I usually use:

try:
f = open(file)
contents = f.read()
finally:
f.close()



以上相当于:

打开(文件){| f |
contents = f.read
}

处理所有事情的逻辑被封装在一起。

但可以用更少的红宝石方式完成:)

lopex



this above is equivalent to:

open(file){|f|
contents=f.read
}

the logic taking care of everything is encapsulated in open.

but can be done in less ruby way way :)

lopex




哎呀我以为我写的是clruby:D


对不起垃圾邮件


lopex



Oops I thought I was writing to c.l.ruby :D

sorry for spam

lopex


Sandra-24写道:
Sandra-24 wrote:
我最近正在阅读一些python代码,我看到了这样的内容:

contents = open(file).read()

当然你也可以:

open(文件,w)。写(obj)

为什么他们没有关闭文件?这个草率的编程还是
文件在引用被销毁时自动关闭(在此
行之后)?


两者!


通常情况下,文件可能会关闭*如果*您正在使用CPython。但是,

绝对不能保证这种行为。例如,Jython使用

不同的垃圾收集方案,文件将立即关闭。

CPython的未来版本也可能有不同的行为。 />
我通常使用:

尝试:
f =打开(文件)
contents = f.read()
最后: f.close()

但现在我想知道这是否是同一件事。你宁愿使用哪种方法?为什么?
I was reading over some python code recently, and I saw something like
this:

contents = open(file).read()

And of course you can also do:

open(file, "w").write(obj)

Why do they no close the files? Is this sloppy programming or is the
file automatically closed when the reference is destroyed (after this
line)?
Both!

Usually, the files will probably be closed *if* you are using CPython. However,
there is absolutely no guarantee of this behavior. For example, Jython uses a
different garbage collection scheme, and the files will *not* close immediately.
Future versions of CPython may have different behavior, too.
I usually use:

try:
f = open(file)
contents = f.read()
finally:
f.close()

But now I am wondering if that is the same thing. Which method would
you rather use? Why?




请继续做你正在做的事情。


-

Robert Kern
ro*********@gmail.com

我已经开始相信整个世界都是一个谜,一个无害的谜团

由于我们疯狂地试图解释它而变得可怕有一个潜在的事实。

- Umberto Eco



Just keep doing what you are doing, please.

--
Robert Kern
ro*********@gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco


这篇关于打开文件而不关闭它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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