How to get a directory file descriptor?

D'Arcy J.M. Cain darcy at druid.net
Tue Nov 25 15:31:31 CET 2008


On Tue, 25 Nov 2008 16:02:56 +0800
Cong Ma <cma at mail.bnu.edu.cn> wrote:
> >> Can you give me some hint on getting a directory file descriptor in Python?
> > for each in os.listdir(os.getcwd()):
> >   print each
> Your code fetches a bunch of strings representing file names in the working
> directory, which is fine. But what I want is something like an integer file
> descriptor, like the one returned by os.open() for files, or the Linux dirfd()
> call, which returns an integer for a pointer to a DIR stream.

Is this what you want?

ofiles = [open(x) for x in os.listdir(os.getcwd())]

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list