What can lead to “IOError: [Errno 9] Bad file descriptor” during 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: 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 … Read more

What’s the difference between a file descriptor and file pointer?

A file descriptor is a low-level integer “handle” used to identify an opened file (or socket, or whatever) at the kernel level, in Linux and other Unix-like systems. You pass “naked” file descriptors to actual Unix calls, such as read(), write() and so on. A FILE pointer is a C standard library-level construct, used to represent a file. The FILE wraps the … Read more