Skip to content Skip to sidebar Skip to footer

How Do I Close Files In Python When I Haven't Got The File Identifiers

I found myself unable to open new files in Python. When I examined with ls -l /proc/PID/fd I saw loads of files open for the Python process. The module I am using is apparently ope

Solution 1:

The problem is likely that those file descriptors are leaking without being associated to Python objects. Python has no way of seeing actual file descriptors (OS resources) that are not associated with Python objects. If they were associated with Python objects, Python would close them when they are garbage collected. Alternatively, the third-party library does its own tracking of file descriptors.

You can use os.close on plain integers to close the associated file descriptor. If you know which file descriptors you want to keep open (usually, stdin/stdout/stderr, which are 0, 1 and 2, and maybe a few others), you can just close all other integers from 0 to 65535, or simply those in /proc/<pid>/fd:

import os

KEEP_FD = set([0, 1, 2])

for fd in os.listdir(os.path.join("/proc", str(os.getpid()), "fd")):
    ifint(fd) notin KEEP_FD:
        try:
            os.close(int(fd))
        except OSError:
            pass

This is a pretty evil hack, though. The better solution would be to fix the third-party library.

Post a Comment for "How Do I Close Files In Python When I Haven't Got The File Identifiers"