在Python文件对象上使用迭代器时,是否需要close()

时间:2009-12-02 12:08:43

标签: python file iterator

执行以下操作并且显式处理文件对象并调用其close()方法是不好的做法?

for line in open('hello.txt'):
    print line

注意 - 这是针对尚未拥有with语句的Python版本。

我问,因为Python文档似乎推荐这个: -

f = open("hello.txt")
try:
    for line in f:
        print line
finally:
    f.close()

这似乎比必要的更冗长。

8 个答案:

答案 0 :(得分:64)

在处理文件时,

关闭始终,在整个地方留下打开的文件句柄并不是一个好主意。当文件对象被垃圾收集时,它们最终会被关闭,但你不知道什么时候会被收集,同时你将通过持有你不再需要的文件句柄来浪费系统资源。

如果您使用的是Python 2.5及更高版本,则可以使用close()语句自动为您调用with

from __future__ import with_statement # Only needed in Python 2.5
with open("hello.txt") as f:
    for line in f:
        print line

这与您拥有的代码具有相同的效果:

f = open("hello.txt")
try:
    for line in f:
        print line
finally:
    f.close()

with语句是对C ++中常用的Resource Acquisition Is Initialization习语的直接语言支持。它允许安全使用和清理各种资源,例如,它可以用于始终确保数据库连接已关闭或锁定始终如下所示。

mylock = threading.Lock()
with mylock:
    pass # do some thread safe stuff

答案 1 :(得分:18)

实际上,文件将在garbage collected时关闭。有关其工作原理的详情,请参阅this question

但仍建议您使用try / finally块或with语句。如果在使用其中一个文件对象的方法时发生异常,则引用将存储在回溯中(存储为全局变量),直到您清除它或发生另一个异常为止。

因此,依靠垃圾收集为您关闭文件是不好的。

此外,如果您已写入该文件,则无法保证更改将在关闭或刷新之前保存到文件中。

答案 2 :(得分:11)

奇怪的是,对于本主题中关于释放系统资源的重要性的所有讨论,没有人提到在我看来确定性地关闭文件的明显更重要的原因:以便可以再次打开它。

肯定存在无关紧要的情况。如果文件对象超出范围或被删除,则基础文件将关闭。 (当它关闭时取决于您正在使用的Python的具体实现。)这通常会足够好 - 如果您确切知道文件变量何时超出范围,并且 if 你知道你不关心文件是否确定性地关闭。

但是,当with陈述存在时,为什么你甚至会为这种分析烦恼呢?

答案 3 :(得分:5)

它在所有地方都有所暗示,但为了使其最清晰,是的,你需要关闭该文件。在Python 2.5(使用 future )和Python 2.6中,您不再需要罗嗦版本:

from __future__ import with_statement
with open("hello.txt") as f:
    for line in f:
        print line

答案 4 :(得分:4)

退出Python解释器(或发生崩溃时的内核)将关闭文件,但是当你不需要它时关闭它们仍然是一个很好的做法。对于1或2或10个文件,它可能不是问题,但是对于更多它可能会导致整个系统崩溃。

最重要的是,这表明编写代码的人实际上关心关于他的工作。

答案 5 :(得分:3)

是的,否则你可能会泄漏资源。

来自Python docs

  

完成文件后,请致电f.close()关闭文件并释放打开文件占用的所有系统资源。

当程序退出时会发生这种情况,但是否则Python会保留它到目前为止不再需要的资源。

答案 6 :(得分:3)

不,我不相信更长的成语是必要的,这就是为什么:

我为模式'for\s+.*\s+in\s+open\(' grepped /usr/lib/python2.6/

找到了很多例子
for line in open('hello.txt'):
    print line

到目前为止

的零实例
f = open("hello.txt")
try:
    for line in f:
        print line
finally:
    f.close()

请参阅下文,了解标准库中使用for ... in open成语的文件列表。

这自然会引出一个问题:如果Python开发人员接受了更短的习语 标准库,我们如何通过使用某些东西来改进任何东西 如果我们的代码依赖于标准库,我们自己的代码会有什么不同?

我认为答案是,较长的成语不能改善任何事情。

我也跑了

#!/usr/bin/env python
try:
    for i,line in enumerate(open('a')):
        print line
        raw_input()
        if i==5:
            break
except Exception:
    pass

raw_input()

并检查/proc/PID/fd文件描述符何时关闭。 看来,当您退出for循环时,文件将为您关闭。

在这些实验的基础上,我不相信长久 try...finally...close成语是必要的。

以下是grep:

的结果
/usr/lib/python2.6/dist-packages/NvidiaDetector/nvidiadetector.py:89:tempList = [ x.strip() for x in open(obsolete).readlines() ]
/usr/lib/python2.6/dist-packages/rpy_io.py:49:for line in open(file).readlines():
/usr/lib/python2.6/dist-packages/setuptools/command/easy_install.py:1376:for line in open(self.filename,'rt'):
/usr/lib/python2.6/dist-packages/GDebi/DscSrcPackage.py:47:for line in open(file):
/usr/lib/python2.6/dist-packages/aptsources/distinfo.py:220:[x.strip() for x in open(value)])
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeCache.py:989:for line in open("/proc/mounts"):
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeAufs.py:100:for line in open("/proc/mounts"):
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeAufs.py:205:for line in open("/proc/mounts"):
/usr/lib/python2.6/dist-packages/DistUpgrade/distinfo.py:220:[x.strip() for x in open(value)])
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeViewKDE.py:826:for c in open(sys.argv[2]).read():
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeConfigParser.py:45:items = [x.strip() for x in open(p)]
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeQuirks.py:684:for line in open(cpuinfo):
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeQuirks.py:692:for line in open("/proc/mounts"):
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeQuirks.py:726:for line in open("/etc/fstab"):
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeQuirks.py:762:for line in open(fstab):
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeQuirks.py:801:for line in open("/etc/fstab"):
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeQuirks.py:874:for line in open(XORG):
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeQuirks.py:939:for line in open(os.path.join(modaliasesdir,filename)):
/usr/lib/python2.6/dist-packages/DistUpgrade/DistUpgradeController.py:1307:for line in open(template):
/usr/lib/python2.6/dist-packages/DistUpgrade/xorg_fix_proprietary.py:23:for raw in open(xorg_source):
/usr/lib/python2.6/dist-packages/DistUpgrade/xorg_fix_proprietary.py:58:for line in open(xorg):
/usr/lib/python2.6/dist-packages/DistUpgrade/xorg_fix_proprietary.py:82:for line in open(xorg):
/usr/lib/python2.6/dist-packages/jockey/oslib.py:377:for line in open(self.apt_jockey_source):
/usr/lib/python2.6/dist-packages/jockey/oslib.py:393:for line in open(f):
/usr/lib/python2.6/dist-packages/jockey/backend.py:651:for line in open(path):
/usr/lib/python2.6/dist-packages/jockey/detection.py:277:for line in open(alias_file):
/usr/lib/python2.6/dist-packages/jockey/detection.py:597:for l in open(os.path.join(path, 'uevent')):
/usr/lib/python2.6/dist-packages/apt/cdrom.py:83:for line in open(fname):
/usr/lib/python2.6/dist-packages/problem_report.py:1119:for line in open('/proc/mounts'):
/usr/lib/python2.6/dist-packages/apport/packaging_impl.py:128:for line in open(f):
/usr/lib/python2.6/dist-packages/apport/packaging_impl.py:190:for line in open(sumfile):
/usr/lib/python2.6/dist-packages/apport/packaging_impl.py:641:for l in open('/etc/apt/sources.list'):
/usr/lib/python2.6/dist-packages/apport/hookutils.py:190:for line in open('/proc/asound/cards'):
/usr/lib/python2.6/dist-packages/apport/hookutils.py:290:for line in open('/var/log/syslog'):
/usr/lib/python2.6/dist-packages/apport/hookutils.py:493:mods = [l.split()[0] for l in open(module_list)]
/usr/lib/python2.6/dist-packages/softwareproperties/SoftwareProperties.py:597:for line in open(f):
/usr/lib/python2.6/dist-packages/softwareproperties/gtk/SoftwarePropertiesGtk.py:883:for x in open(tmp.name):
/usr/lib/python2.6/dist-packages/lsb_release.py:253:for line in open('/etc/lsb-release'):
/usr/lib/python2.6/dist-packages/numpy/distutils/system_info.py:815:for d in open(ld_so_conf,'r').readlines():
/usr/lib/python2.6/dist-packages/LanguageSelector/LocaleInfo.py:72:for line in open(languagelist_file):
/usr/lib/python2.6/dist-packages/LanguageSelector/LocaleInfo.py:187:for line in open(environment).readlines():
/usr/lib/python2.6/dist-packages/LanguageSelector/LocaleInfo.py:193:for line in open(environment).readlines():
/usr/lib/python2.6/dist-packages/LanguageSelector/LanguageSelector.py:125:for line in open(fname):
/usr/lib/python2.6/dist-packages/LanguageSelector/LanguageSelector.py:140:for line in open(fname):
/usr/lib/python2.6/dist-packages/LanguageSelector/LanguageSelector.py:171:for line in open(fname):
/usr/lib/python2.6/dist-packages/LanguageSelector/LanguageSelector.py:210:for line in open(fname):
/usr/lib/python2.6/dist-packages/LanguageSelector/macros.py:16:for l in open(file):
/usr/lib/python2.6/dist-packages/LanguageSelector/macros.py:37:for l in open(self.LANGCODE_TO_LOCALE):
/usr/lib/python2.6/dist-packages/LanguageSelector/LangCache.py:94:for l in open(self.BLACKLIST):
/usr/lib/python2.6/dist-packages/LanguageSelector/LangCache.py:99:for l in open(self.LANGCODE_TO_LOCALE):
/usr/lib/python2.6/dist-packages/LanguageSelector/LangCache.py:111:for l in open(self.PACKAGE_DEPENDS):
/usr/lib/python2.6/dist-packages/LanguageSelector/ImSwitch.py:78:for l in open(self.blacklist_file):

答案 7 :(得分:2)

您需要关闭句柄以释放内存。在一次处理大量文件之前不需要。