os.listdir() bug in WinXp. Calling with empty string

John Roth newsgroups at jhrothjr.com
Sun Oct 5 07:29:34 EDT 2003


"Terry Reedy" <tjreedy at udel.edu> wrote in message
news:NK-dnYuNtPlipuKiU-KYuA at comcast.com...
>
> "Hannu Kankaanp??" <hanzspam at yahoo.com.au> wrote in message
> news:840592e1.0310040736.83c9bc3 at posting.google.com...
> > This may be a bug or simply a strange result of undefined
> > behaviour, but this is what I get with Python 2.3.2 on
> > Windows XP:
> >
> > >>> import os
> > >>> os.listdir('')
> > [ filenames in the current directory, e.g. c:\python\ ]
> > >>> os.listdir(u'')
> > [ filenames in the drive's root directory, e.g. c:\ ]
>
> On Win98, *both* calls list root directory.  listdir(path) must pass
> path to os system call and print response or raise exception as
> determined by os response.
>
> >Maybe os.listdir() behaviour is undefined for empty strings,
>
> It seems to be defined as whatever the os does with empty strings,
> which is not much more useful.  You could open SF bug report listing
> various XP, W98, Linux behaviors and suggest that listdir def be
> changed to 'if path: current behaviour; else: raise exception'.

I'd like to argue *against* raising an exception. We had a long
discussion on this exact point on the XP list a while ago, with
Ron Jeffries holding out for the position that if you can define
a useful behavior for a corner case, then you should do so
rather than raising an exception. The least surprising behavior
here is to simply list the current directory. In other words,
a null string would be equivalent to a string containing a single
dot.

John Roth
>
> Terry J. Reedy
>
>






More information about the Python-list mailing list