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

  • Thread starter Hannu Kankaanp??
  • Start date
H

Hannu Kankaanp??

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:\ ]

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:
os.listdir('.') [ filenames in the current directory, e.g. c:\python\ ]
os.listdir(u'.')
[ filenames in the current directory, e.g. c:\python\ ]
 
G

Gerrit Holl

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:\ ]

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
 
T

Terry Reedy

Hannu Kankaanp?? said:
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'.

Terry J. Reedy
 
B

Bengt Richter

Hannu Kankaanp?? said:
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
 
H

Hannu Kankaanp??

Terry Reedy said:
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'.

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).
 
J

John Roth

Terry Reedy said:
Hannu Kankaanp?? said:
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
 
T

Terry Reedy

John Roth said:
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.

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
 
B

Bengt Richter

Terry Reedy said:
Hannu Kankaanp?? said:
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.
+1

That intuitively matches well with arg-less invocation of ls or dir.

Regards,
Bengt Richter
 
B

Bernhard Herzog

+1

That intuitively matches well with arg-less invocation of ls or dir.

-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
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 2] No such file or directory: ''


Bernhard
 
B

Bengt Richter

-1

Passing an empty string isn't an argless call.
True. I meant "argless" as in nothing more on the shell command line following ls or dir ;-)
If you want os.listdir to be similar to ls, os.listdir() should be
equivalent to os.listdir(".") (currently the string argument is
required).
I agree, that would be cleaner, and easy to do with just path='.' as default arg.
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
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 2] No such file or directory: ''

But we also saw different behavior for u'' on various platforms,
so the question of normalizing empty-string behavior platform-independently
for Python comes up. Maybe a ValueError exception consistently would be best,
on the assumption that you should not normally generate that parameter value.

I haven't bumped into the problem in a real use case, so I have no big
preference other than consistent platform independent behavior.

Regards,
Bengt Richter
 
B

Bernhard Herzog

True. I meant "argless" as in nothing more on the shell command line
following ls or dir ;-)

That's also how I understood it.

[...]
But we also saw different behavior for u'' on various platforms,
so the question of normalizing empty-string behavior platform-independently
for Python comes up.

Certainly. I just don't think that the empty string should mean the
current directory, although one could argue that it would be consistent
with the interpretation of an empty string in sys.path.
Maybe a ValueError exception consistently would be best,
+1

I haven't bumped into the problem in a real use case,

I haven't either but ISTM that trying to use an empty string as a
directory name is likely the result of some error and thus should
produce an exception.

One scenario I can think if would be config files as read by
ConfigParser. Say you have a section which among other things contains
an option that is a directory name. If you want to explicitly set that
option to a Null value you could simply omit the value:

[images]
dir=


ConfigParser would give you an empty string:
import ConfigParser
c = ConfigParser.ConfigParser()
c.read(["dummy.config"])
c.get("images", "dir") ''

Should that really mean "current directory" instead of "no directory
given"?

Bernhard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top