简体   繁体   English

Python:如果我用打开文件功能指定stdout,它会自动关闭吗?

[英]Python: If I specify stdout with open file function does it close automatically?

Example: 例:

subprocess.call(cmd, stdout=open('status_grid','a'), cwd = folder)

is the file status_grid closed automatically? status_grid文件是否自动关闭?

No, it doesn't: 不,不是:

import subprocess
f = open('b','a')
subprocess.call('ls', stdout=f)
print f.closed

Output: 输出:

False

Now a better answer might come from unutbu. 现在,更好的答案可能来自unutbu。 You don't give your open file a reference, so once your subprocess completes, it's up to the garbage collector how much longer the file is open. 您没有给打开的文件提供参考,因此,一旦子进程完成,则由垃圾收集器决定打开文件的时间。

One way to be sure is 一种确定的方法是

with open('status_grid', 'a') as my_file:
    subprocess.call(cmd, stdout=my_file, cwd = folder)

If not done explicitly, the file will be closed when it is garbage collected. 如果未明确完成,则在垃圾回收时将关闭文件。 When the file is garbage collected is not specified by the Python language per se . Python语言本身未指定何时垃圾收集文件。

In CPython, the file is garbage collected when there are no more references to the file object. 在CPython中,当不再有对该文件对象的引用时,将对该文件进行垃圾回收。

With other implementations of Python, such as Jython, garbage collection may happen completely differently : 使用Jython等其他Python实现,垃圾回收的发生方式可能完全不同

Jython has "true" garbage collection whereas CPython uses reference counting. Jython具有“真实的”垃圾回收,而CPython使用引用计数。 This means that in Jython users don't need to worry about handling circular references as these are guaranteed to be collected properly. 这意味着在Jython中,用户不必担心处理循环引用,因为可以保证可以正确收集这些循环引用。 On the other hand, users of Jython have no guarantees of when an object will be finalized -- this can cause problems for people who use open("foo", 'r').read() excessively. 另一方面,Jython的用户不能保证何时结束对象-这可能给过度使用open(“ foo”,'r')。read()的人造成麻烦。 Both behaviors are acceptable -- and highly unlikely to change. 两种行为都是可以接受的-并且极不可能改变。


As EMS and Charles Salvia point out, to be sure when the file is closed, it is best to not leave it up to the garbage collector. EMS和Charles Salvia指出,为确保关闭文件时,最好不要将其留给垃圾收集器处理。 The best way to do that is to use a with statement , which guarantees the file will be closed when Python leaves the with-suite : 最好的方法是使用with语句 ,该语句可确保在Python离开with-suite时关闭文件:

with open('status_grid','a') as f:
    subprocess.call(cmd, stdout=f, cwd = folder)

No it's not. 不,这不对。 You can wrap your call in a with statement to ensure the file closes automatically: 您可以将调用包装在with语句中,以确保文件自动关闭:

with open('status_grid','a') as myfile:
   subprocess.call(cmd, stdout=myfile, cwd = folder)

Note: with current CPython implementations based on reference counting, the file will be closed when the reference count reaches 0, which will happen immediately in the code you posted. 注意:在当前基于引用计数的CPython实现中,当引用计数达到0时,文件将关闭,这将立即在您发布的代码中发生。 However, this is just an implementation detail of CPython. 但是,这只是CPython的实现细节。 Other implementations may leave the file open indefinitely. 其他实现可能会无限期地打开文件。 Use the with statement to ensure you've written portable code. 使用with语句可确保您已编写了可移植的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM