[Python-Dev] Remaining decisions on PEP 471 -- os.scandir()

Cameron Simpson cs at zip.com.au
Wed Jul 16 05:40:00 CEST 2014

I was going to stay out of this one...

On 14Jul2014 10:25, Victor Stinner <victor.stinner at gmail.com> wrote:
>2014-07-14 4:17 GMT+02:00 Nick Coghlan <ncoghlan at gmail.com>:
>> Or the ever popular symlink to "." (or a directory higher in the tree).
>"." and ".." are explicitly ignored by os.listdir() an os.scandir().
>> I think os.walk() is a good source of inspiration here: call the flag
>> "followlink" and default it to False.

I also think followslinks should be spelt like os.walk, and also default to 

>IMO the specific function os.walk() is not a good example. It includes
>symlinks to directories in the dirs list and then it does not follow

I agree that is a bad mix.

>it is a recursive function and has a followlinks optional
>parameter (default: False).

Which I think is desirable.

>Moreover, in 92% of cases, functions using os.listdir() and
>os.path.isdir() *follow* symlinks:


This is a historic artifact, a convenience, and a side effect of bring symlinks 
into UNIX in the first place.

The objective was that symlinks should largely be transparent to users for 
naive operation. So the UNIX calls open/cd/listdir all follow symlinks so that 
things work transparently and a million C programs do not break. 

However, so do chmod/chgrp/chown, for the same reasons and with generally less 
desirable effects.

Conversely, the find command, for example, does not follow symlinks and this is 
generally a good thing. "ls" is the same. Like os.walk, they are for inspecting 
stuff, and shouldn't indirect unless asked.

I think following symlinks, especially for something like os.walk and 
os.scandir, should default to False. I DO NOT want to quietly wander to remote 
parts of the file space because someone has stuck a symlink somewhere 
unfortunate, lurking like a little bomb (or perhaps trapdoor, waiting to suck 
me down into an unexpected dark place).

It is also slower to follow symlinks by default.

I am also against flag parameters that default to True, on the whole; they are 
a failure of ergonomic design. Leaving off a flag should usually be like 
setting it to False. A missing flag is an "off" flag.

For these reasons (and others I have not yet thought through:-) I am voting for 


optional parameter.

If you want to follow links, it is hardly difficult.

Cameron Simpson <cs at zip.com.au>

Our job is to make the questions so painful that the only way to make the
pain go away is by thinking.    - Fred Friendly

More information about the Python-Dev mailing list