Finding count of currently open file descriptors.

Andreas Kostyrka andreas at
Thu Aug 1 10:37:17 CEST 2002

Am Die, 2002-07-30 um 16.31 schrieb Tim McNerney:
> > In a Python debug build, you can find out all file objects, but there
> > may be file descriptors not controlled by Python file objects (e.g. if
> > opened by
> This is more what I'm looking for -- some generic method which is within 
> Python itself. How would I go about doing this within the debug build?
Well, an "easy" way would be:
for i in xrange(64*1024):
		do something with fd
	except IOError:

For do something, you can use fstat:
>>> def countfd():
...     count=0
...     for i in xrange(64*1024):
...             try:
...                     os.fstat(i)
...                     count+=1
...             except os.error:
...                     pass
...     return count
>>> countfd()
>>> f=open("/dev/null")
>>> countfd()

Only problem, scanning 64K descriptors take some seconds or so here.
Perhaps in your situation you could limit somehow the number that must
be scanned.

Even better, use isatty, on my Linux (Duron700 SonyFX201) system it's
tenfold faster:
isatty: 0.308787202835
fstat : 3.40730090141

I'l email you privatly the module with the countfd function.



More information about the Python-list mailing list