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

Hans Mulder hansmu at xs4all.nl
Thu Sep 20 04:30:30 EDT 2012


On 20/09/12 05:11:11, Chris Angelico wrote:
> 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.

On MacOS X, you can use

    os.listdir("/dev/fd")

This might work on other OSes.

> 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).

At least on Linux, Solaris and MacOS X, you shouldd use /dev/stdin,
which is a symlink to whatever the official name is.
"/dev/stdin" is more readable and more portable than the official name.

> So I'm confident enough to recommend testing it. :)

Testing such things is the best way to learn.


Hope this helps,

-- HansM




More information about the Python-list mailing list