Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm using os.system(), which is a scientific software including a python script, used to run another scientific program. There's a subprocess, which will be running and python will print the following:

close failed in file object destructor:

IOError: [Errno 9] Bad file descriptor

Even this message will be printed at the same time as os.system()?

But my question is, Which condition will lead to this type of IOError? What is it? 

1 Answer

0 votes
by (26.4k points)

You get this kind of error message only if python file was closed from the outside, i.e.,not from the file objects close() :

>>> f = open(".bashrc")

>>> os.close(f.fileno())

>>> del f

close failed in file object destructor:

IOError: [Errno 9] Bad file descriptor

In the above code, del f line will delete the last reference to the file object, Ultimately file.__del__ will be called. 

The Internal state indicates that the file is still open since f.close() was never called, So here someone tries to close the file, that someone is destructor here. Later, OS throws an error because of the attempt for closing the file, which is not open.

Since os.system() does not create any python file object, It doesn't look like system() call was the origin of the error

 

Are you interested to learn Python, then Check out: Python training course

Related questions

0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 26, 2020 in Python by ashely (50.2k points)

Browse Categories

...