导致"IOError:[Errno 9]错误的文件描述符"的原因是什么?在os.system()期间? [英] What can lead to "IOError: [Errno 9] Bad file descriptor" during os.system()?

查看:1607
本文介绍了导致"IOError:[Errno 9]错误的文件描述符"的原因是什么?在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()?

推荐答案

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

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?

这篇关于导致"IOError:[Errno 9]错误的文件描述符"的原因是什么?在os.system()期间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆