How to get the list of all my open file(descriptor)s and locks?

Chris Angelico rosuav at gmail.com
Wed Sep 19 23:11:11 EDT 2012


On Thu, Sep 20, 2012 at 7:09 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> You could do:
>
> os.listdir("/proc/%d/fd" % os.getpid())
>
> This should work on Linux, AIX, and Solaris, but obviously not on Windows.

I'm not sure how cross-platform it is, but at least on Linux, you can
use /proc/self as an alias for "/proc/"+os.getpid() - worth a try,
might make your code a bit easier to read.

It's documented as useful when you don't know the PID yet (eg telling
a process to read the file /proc/self/fd/0 to force it to use stdin).
So I'm confident enough to recommend testing it. :)

ChrisA



More information about the Python-list mailing list