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

Bengt Richter bokr at oz.net
Sat Oct 4 20:25:53 EDT 2003


On Sat, 4 Oct 2003 17:30:37 -0400, "Terry Reedy" <tjreedy at udel.edu> wrote:

>
>"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'.
>
Another datapoint, for NT4:

 [17:29] V:\msys\1.0\home\bokr\test>python
 Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import os, sys
 >>> sys.version_info
 (2, 3, 0, 'final', 0)
 >>> print os.listdir('')
 ['hellow.c', 'hellow.exe', 'hellow.i', 'hellow.o', 'hellow.s', 'hw.cpp', 'hw.exe']
 >>> print os.listdir(u'')[:4]
 [u'19990913-test1.txt.pgp', u'19990913-test2.txt.pgp', u'2000-2-7-Scans', u'AGENT']
 >>> print os.listdir('\\')[:4]
 ['19990913-test1.txt.pgp', '19990913-test2.txt.pgp', '2000-2-7-Scans', 'AGENT']
 >>> print os.listdir(u'.')[:4]
 [u'hellow.c', u'hellow.exe', u'hellow.i', u'hellow.o']
 >>> print os.listdir(u'\\')[:4]
 [u'19990913-test1.txt.pgp', u'19990913-test2.txt.pgp', u'2000-2-7-Scans', u'AGENT']

so it appears u'' effectively becomes u'\\'), but
'' becomes '.' for NT4 and python 2.3.0.
(I better upgrade to 2.3.2, I guess. (And clean up v:'s root dir ;-).




Regards,
Bengt Richter




More information about the Python-list mailing list