什么会导致“IOError: [Errno 9] Bad file descriptor"?在 os.system() 期间?
问题描述
我正在使用一个科学软件,其中包括一个调用 os.system()
的 Python 脚本,该脚本用于运行另一个科学程序.当子进程运行时,Python 有时会打印以下内容:
I am using a scientific software including a Python script that is calling os.system()
which is used to run another scientific program. While the subprocess is running, Python at some point prints the following:
close failed in file object destructor:
IOError: [Errno 9] Bad file descriptor
我相信这条消息是在 os.system()
返回的同时打印出来的.
I believe that this message is printed at the same time as os.system()
returns.
我现在的问题是:
哪些情况会导致这种类型的 IOError?它到底是什么意思?os.system()
调用的子进程是什么意思?
Which conditions can lead to this type of IOError? What does it exactly mean? What does it mean for the subprocess that has been invoked by os.system()
?
解决方案
如果 Python 文件从外部"关闭,即不是从文件对象的 close()
关闭,则会收到此错误消息方法:
You get this error message if a Python file was closed from "the outside", i.e. not from the file object's close()
method:
>>> f = open(".bashrc")
>>> os.close(f.fileno())
>>> del f
close failed in file object destructor:
IOError: [Errno 9] Bad file descriptor
del f
行删除了对文件对象的最后一个引用,导致其析构函数 file.__del__
被调用.文件对象的内部状态表明文件仍然打开,因为 f.close()
从未被调用,因此析构函数尝试关闭文件.由于试图关闭未打开的文件,操作系统随后会引发错误.
The line del f
deletes the last reference to the file object, causing its destructor file.__del__
to be called. The internal state of the file object indicates the file is still open since f.close()
was never called, so the destructor tries to close the file. The OS subsequently throws an error because of the attempt to close a file that's not open.
由于 os.system()
的实现不会创建任何 Python 文件对象,因此 system()
调用似乎不太可能是错误.也许您可以显示更多代码?
Since the implementation of os.system()
does not create any Python file objects, it does not seem likely that the system()
call is the origin of the error. Maybe you could show a bit more code?
相关文章