python - 在 os.system() 期间什么会导致 "IOError: [Errno 9] Bad file descriptor"?

标签 python subprocess posix file-descriptor ioerror

我正在使用一个科学软件,其中包括一个调用 os.system() 的 Python 脚本,该脚本用于运行另一个科学程序。当子进程运行时,Python 有时会打印以下内容:

close failed in file object destructor:
IOError: [Errno 9] Bad file descriptor

我相信这条消息是在 os.system() 返回的同时打印出来的。

我现在的问题是:

哪些情况会导致这种类型的 IOError?它到底是什么意思? os.system()调用的子进程是什么意思?

最佳答案

如果 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() 调用似乎不太可能是错误。也许您可以显示更多代码?

关于python - 在 os.system() 期间什么会导致 "IOError: [Errno 9] Bad file descriptor"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7686275/

相关文章:

Python 单元测试模块化与可读性

java - 考虑到输入和输出,如何使用 python 执行 java 程序

macos - 提供字符串变量而不是文件路径作为命令行参数

c - POSIX 返回并打印数组 prthread_join()?

c - 在 ARM 平台上,如何访问在内存中正确对齐的字符串的各个字符?

python - 使用 Keras ImageDataGenerator 预测进行索引会引发不可散列类型错误

python - 如何将 VB.NET 程序与 Python 连接

python - 如何在 python 中使用索引按 "/"分隔符分割?

python - 为什么这个 python 脚本只能在 Ubuntu 12.04 上的 IDLE 下工作?

python - 使用 subprocess.call 在内存中收集 stderr