什么可能导致“IOError:[Errno 9]错误的文件描述符”?在 os.system() 期间?

发布于 2024-12-08 22:05:40 字数 326 浏览 1 评论 0原文

我正在使用一个科学软件,其中包括一个调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

公布 2024-12-15 22:05:40

如果 Python 文件是从“外部”关闭的,即不是从文件对象的 close() 方法关闭的,您会收到此错误消息:

>>> 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(),因此析构函数会尝试关闭文件。由于尝试关闭未打开的文件,操作系统随后会引发错误。

由于 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:

>>> f = open(".bashrc")
>>> os.close(f.fileno())
>>> del f
close failed in file object destructor:
IOError: [Errno 9] Bad file descriptor

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.

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?

我还不会笑 2024-12-15 22:05:40

如果打开文件时使用错误的模式,您可能会收到此错误。例如:

with open(output, 'wb') as output_file:
    print(output_file.read())

在该代码中,我想读取文件,但我使用模式 wb 而不是 rr+

You can get this error if you use wrong mode when opening the file. For example:

with open(output, 'wb') as output_file:
    print(output_file.read())

In that code, I want to read the file, but I use mode wb instead of r or r+

夏末染殇 2024-12-15 22:05:40

如果您的工作目录启用了勒索软件防护,您可能会收到此错误。如果目录受 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文