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

What can lead to quot;IOError: [Errno 9] Bad file descriptorquot; during os.system()?(什么会导致“IOError: [Errno 9] Bad file descriptor?在 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() 期间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)