什么可能导致“IOError:[Errno 9]错误的文件描述符”?在 os.system() 期间?
我正在使用一个科学软件,其中包括一个调用 os.system() 的 Python 脚本,该脚本用于运行另一个科学程序。当子进程运行时,Python 在某个时刻会打印以下内容:
close failed in file object destructor:
IOError: [Errno 9] Bad file descriptor
我相信此消息是在 os.system() 返回的同时打印的。
我现在的问题是:
哪些情况会导致这种类型的 IOError?它究竟意味着什么?对于 os.system() 调用的子进程来说,这意味着什么?
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
I believe that this message is printed at the same time as os.system()
returns.
My questions now are:
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()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 Python 文件是从“外部”关闭的,即不是从文件对象的
close()
方法关闭的,您会收到此错误消息:del f
行删除了最后一个引用到文件对象,导致其析构函数file.__del__
被调用。文件对象的内部状态表明文件仍处于打开状态,因为从未调用过 f.close(),因此析构函数会尝试关闭文件。由于尝试关闭未打开的文件,操作系统随后会引发错误。由于
os.system()
的实现不会创建任何 Python 文件对象,因此system()
调用似乎不太可能是错误的根源。也许您可以展示更多代码?You get this error message if a Python file was closed from "the outside", i.e. not from the file object's
close()
method:The line
del f
deletes the last reference to the file object, causing its destructorfile.__del__
to be called. The internal state of the file object indicates the file is still open sincef.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.Since the implementation of
os.system()
does not create any Python file objects, it does not seem likely that thesystem()
call is the origin of the error. Maybe you could show a bit more code?如果打开文件时使用错误的模式,您可能会收到此错误。例如:
在该代码中,我想读取文件,但我使用模式
wb
而不是r
或r+
You can get this error if you use wrong mode when opening the file. For example:
In that code, I want to read the file, but I use mode
wb
instead ofr
orr+
如果您的工作目录启用了勒索软件防护,您可能会收到此错误。如果目录受 Windows 附带的勒索软件保护保护,则 Windows 不允许任何第三方应用程序对文件进行更改。您可以通过转到“Windows 安全 -> 病毒和威胁防护 -> 管理勒索软件防护 -> 允许应用程序通过受控文件夹访问”来纠正此问题,
然后通过单击“添加允许的应用程序”添加“Python[版本].exe” 。
You can get this error if your Ransomware Protection is enabled on your working directory. Windows does not allow any third party applications to make changes to files if the directory is protected by Ransomware Protection which comes with windows. You can rectify this by going to "Windows Security -> Virus and Threat Protection -> Manage Ransomware Protection -> Allow an app through controlled folder access"
Then add "Python[version].exe" by clicking Add an allowed app.