开发者

What can lead to "IOError: [Errno 9] Bad file descriptor" during os.system()?

开发者 https://www.devze.com 2023-04-11 04:47 出处:网络
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

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 th开发者_开发百科at 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()?


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?


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+


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消