How to get a directory file descriptor?

Cong Ma cma at
Wed Nov 26 11:56:01 CET 2008

alex23 wrote:
> On Nov 26, 3:26 pm, greg <g... at> wrote:
>> os.O_DIRECTORY must be fairly new -- it doesn't exist
>> in my 2.5 installation. But os.O_RDONLY seems to work
>> just as well for this purpose.
> Which OS are you using?
> Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
> [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import os
>>>> hasattr(os, 'O_DIRECTORY')
> True
> I'm pretty certain it was present under Windows XP as well.
> --

Thanks for your reply. I checked my Python 2.5 install on Linux and there's the
O_DIRECTORY flag. However this is not mentioned anywhere in the Library Reference.

There's another question regarding to this flag though: I checked the manual of
the Linux system call open(2), in which there's a paragraph on this flag:

              If pathname is not a directory, cause the open  to  fail.   This
              flag is Linux-specific, and was added in kernel version 2.1.126,
              to avoid denial-of-service problems if opendir(3) is called on a
              FIFO  or  tape  device,  but  should  not be used outside of the
              implementation of opendir(3).

Note the "should not be used outside of the implementation of opendir(3)" part.
Does that mean using Python's with the O_DIRECTORY flag is a Bad
Thing? In C, I think, calling opendir() followed by dirfd() should be the
correct way of doing this?


More information about the Python-list mailing list