os.listdir() bug in WinXp. Calling with empty string | | |
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:
[color=blue][color=green][color=darkred]
>>> import os
>>> os.listdir('')[/color][/color][/color]
[ filenames in the current directory, e.g. c:\python\ ][color=blue][color=green][color=darkred]
>>> os.listdir(u'')[/color][/color][/color]
[ filenames in the drive's root directory, e.g. c:\ ]
Maybe os.listdir() behaviour is undefined for empty strings,
but the different behaviour between unicode and normal
strings surprised me. If calling it with an empty string is
illegal, it should throw an exception instead of doing
weird things.
This works fine, though:
[color=blue][color=green][color=darkred]
>>> os.listdir('.')[/color][/color][/color]
[ filenames in the current directory, e.g. c:\python\ ][color=blue][color=green][color=darkred]
>>> os.listdir(u'.')[/color][/color][/color]
[ filenames in the current directory, e.g. c:\python\ ] | | | | re: os.listdir() bug in WinXp. Calling with empty string
<quote name="Hannu Kankaanp??" date="1065252981" email="hanzspam@yahoo.com.au">[color=blue]
> 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:
>[color=green][color=darkred]
> >>> import os
> >>> os.listdir('')[/color][/color]
> [ filenames in the current directory, e.g. c:\python\ ][color=green][color=darkred]
> >>> os.listdir(u'')[/color][/color]
> [ filenames in the drive's root directory, e.g. c:\ ][/color]
Output on Redhat Linux 9.0:
17:43:19:2:gerrit@topjaklont:~/email/2003/39$ python
Python 2.3 (#1, Aug 5 2003, 14:13:25)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
0 >>> import os
1 >>> os.listdir('')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 2] No such file or directory: ''
2 >>> os.listdir(u'')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 2] No such file or directory: ''
Gerrit.
-
Mozilla _is_ the web: it grows faster than you can download it.
1011001 1101111 1110101 1110010 1110011 0101100
1000111 1100101 1110010 1110010 1101001 1110100 | | | | re: os.listdir() bug in WinXp. Calling with empty string
"Hannu Kankaanp??" <hanzspam@yahoo.com.au> wrote in message
news:840592e1.0310040736.83c9bc3@posting.google.co m...[color=blue]
> 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:
>[color=green][color=darkred]
> >>> import os
> >>> os.listdir('')[/color][/color]
> [ filenames in the current directory, e.g. c:\python\ ][color=green][color=darkred]
> >>> os.listdir(u'')[/color][/color]
> [ filenames in the drive's root directory, e.g. c:\ ][/color]
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.
[color=blue]
>Maybe os.listdir() behaviour is undefined for empty strings,[/color]
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'.
Terry J. Reedy | | | | re: os.listdir() bug in WinXp. Calling with empty string
On Sat, 4 Oct 2003 17:30:37 -0400, "Terry Reedy" <tjreedy@udel.edu> wrote:
[color=blue]
>
>"Hannu Kankaanp??" <hanzspam@yahoo.com.au> wrote in message
>news:840592e1.0310040736.83c9bc3@posting.google.c om...[color=green]
>> 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:
>>[color=darkred]
>> >>> import os
>> >>> os.listdir('')[/color]
>> [ filenames in the current directory, e.g. c:\python\ ][color=darkred]
>> >>> os.listdir(u'')[/color]
>> [ filenames in the drive's root directory, e.g. c:\ ][/color]
>
>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.
>[color=green]
>>Maybe os.listdir() behaviour is undefined for empty strings,[/color]
>
>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'.
>[/color]
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.[color=blue][color=green][color=darkred]
>>> import os, sys
>>> sys.version_info[/color][/color][/color]
(2, 3, 0, 'final', 0)[color=blue][color=green][color=darkred]
>>> print os.listdir('')[/color][/color][/color]
['hellow.c', 'hellow.exe', 'hellow.i', 'hellow.o', 'hellow.s', 'hw.cpp', 'hw.exe'][color=blue][color=green][color=darkred]
>>> print os.listdir(u'')[:4][/color][/color][/color]
[u'19990913-test1.txt.pgp', u'19990913-test2.txt.pgp', u'2000-2-7-Scans', u'AGENT'][color=blue][color=green][color=darkred]
>>> print os.listdir('\\')[:4][/color][/color][/color]
['19990913-test1.txt.pgp', '19990913-test2.txt.pgp', '2000-2-7-Scans', 'AGENT'][color=blue][color=green][color=darkred]
>>> print os.listdir(u'.')[:4][/color][/color][/color]
[u'hellow.c', u'hellow.exe', u'hellow.i', u'hellow.o'][color=blue][color=green][color=darkred]
>>> print os.listdir(u'\\')[:4][/color][/color][/color]
[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 | | | | re: os.listdir() bug in WinXp. Calling with empty string
"Terry Reedy" <tjreedy@udel.edu> wrote in message news:<NK-dnYuNtPlipuKiU-KYuA@comcast.com>...[color=blue]
> 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'.[/color]
Ok, did that. There was enough test data already in my opinion,
since it seemed that almost everyone got different results
(only WinNT/WinXP behaviour was the same). | | | | re: os.listdir() bug in WinXp. Calling with empty string
"Terry Reedy" <tjreedy@udel.edu> wrote in message
news:NK-dnYuNtPlipuKiU-KYuA@comcast.com...[color=blue]
>
> "Hannu Kankaanp??" <hanzspam@yahoo.com.au> wrote in message
> news:840592e1.0310040736.83c9bc3@posting.google.co m...[color=green]
> > 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:
> >[color=darkred]
> > >>> import os
> > >>> os.listdir('')[/color]
> > [ filenames in the current directory, e.g. c:\python\ ][color=darkred]
> > >>> os.listdir(u'')[/color]
> > [ filenames in the drive's root directory, e.g. c:\ ][/color]
>
> 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.
>[color=green]
> >Maybe os.listdir() behaviour is undefined for empty strings,[/color]
>
> 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'.[/color]
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[color=blue]
>
> Terry J. Reedy
>
>[/color] | | | | re: os.listdir() bug in WinXp. Calling with empty string
"John Roth" <newsgroups@jhrothjr.com> wrote in message
news:vo004qejvqfcaa@news.supernews.com...[color=blue]
>
> "Terry Reedy" <tjreedy@udel.edu> wrote in message[color=green]
> > 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[/color][/color]
listing[color=blue][color=green]
> > various XP, W98, Linux behaviors and suggest that listdir def be
> > changed to 'if path: current behaviour; else: raise exception'.[/color]
>
> 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.[/color]
You are suggesting to insert "if not path: path= '.'" I would
consider that to also improve over the current situation. You can add
a note to Hannu's bug report to make sure this is considered by
whoever picks it up.
Terry J. Reedy | | | | re: os.listdir() bug in WinXp. Calling with empty string
On Sun, 5 Oct 2003 07:29:34 -0400, "John Roth" <newsgroups@jhrothjr.com> wrote:
[color=blue]
>
>"Terry Reedy" <tjreedy@udel.edu> wrote in message
>news:NK-dnYuNtPlipuKiU-KYuA@comcast.com...[color=green]
>>
>> "Hannu Kankaanp??" <hanzspam@yahoo.com.au> wrote in message
>> news:840592e1.0310040736.83c9bc3@posting.google.co m...[color=darkred]
>> > 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:\ ][/color]
>>
>> 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.
>>[color=darkred]
>> >Maybe os.listdir() behaviour is undefined for empty strings,[/color]
>>
>> 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'.[/color]
>
>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.
>[/color]
+1
That intuitively matches well with arg-less invocation of ls or dir.
Regards,
Bengt Richter | | | | re: os.listdir() bug in WinXp. Calling with empty string bokr@oz.net (Bengt Richter) writes:
[color=blue]
> On Sun, 5 Oct 2003 07:29:34 -0400, "John Roth" <newsgroups@jhrothjr.com> wrote:[color=green]
>> 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.
>>[/color]
> +1
>
> That intuitively matches well with arg-less invocation of ls or dir.[/color]
-1
Passing an empty string isn't an argless call.
If you want os.listdir to be similar to ls, os.listdir() should be
equivalent to os.listdir(".") (currently the string argument is
required).
And for an empty string a brief test shows that ls with an empty string
as argument behaves just like os.listdir("") (both on GNU/Linux system):
$ ls ''
ls: : No such file or directory
[color=blue][color=green][color=darkred]
>>> os.listdir("")[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 2] No such file or directory: ''
Bernhard
--
Intevation GmbH http://intevation.de/
Sketch http://sketch.sourceforge.net/
Thuban http://thuban.intevation.org/ |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|