What can lead to quot;IOError: [Errno 9] Bad file descriptorquot; during os.system()?(什么会导致“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?
这篇关于什么会导致“IOError: [Errno 9] Bad file descriptor"?在 os.system() 期间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么会导致“IOError: [Errno 9] Bad file descriptor"?在 os.system() 期间?
基础教程推荐
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01