473,241 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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:
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\ ]
Jul 18 '05 #1
8 2425
<quote name="Hannu Kankaanp??" date="1065252981" email="ha******@yahoo.com.au">
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

Jul 18 '05 #2

"Hannu Kankaanp??" <ha******@yahoo.com.au> wrote in message
news:84*************************@posting.google.co m...
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
Jul 18 '05 #3
On Sat, 4 Oct 2003 17:30:37 -0400, "Terry Reedy" <tj*****@udel.edu> wrote:

"Hannu Kankaanp??" <ha******@yahoo.com.au> wrote in message
news:84*************************@posting.google.c om...
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
Jul 18 '05 #4
"Terry Reedy" <tj*****@udel.edu> wrote in message news:<NK********************@comcast.com>...
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).
Jul 18 '05 #5

"Terry Reedy" <tj*****@udel.edu> wrote in message
news:NK********************@comcast.com...

"Hannu Kankaanp??" <ha******@yahoo.com.au> wrote in message
news:84*************************@posting.google.co m...
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

Jul 18 '05 #6

"John Roth" <ne********@jhrothjr.com> wrote in message
news:vo************@news.supernews.com...

"Terry Reedy" <tj*****@udel.edu> wrote in message
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.


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
Jul 18 '05 #7
On Sun, 5 Oct 2003 07:29:34 -0400, "John Roth" <ne********@jhrothjr.com> wrote:

"Terry Reedy" <tj*****@udel.edu> wrote in message
news:NK********************@comcast.com...

"Hannu Kankaanp??" <ha******@yahoo.com.au> wrote in message
news:84*************************@posting.google.co m...
> 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
Jul 18 '05 #8

bo**@oz.net (Bengt Richter) writes:
On Sun, 5 Oct 2003 07:29:34 -0400, "John Roth" <ne********@jhrothjr.com> wrote:
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.


-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
os.listdir("")

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/
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jason Kratz | last post by:
OK. I've search on google groups and around the web for this and I haven't found an answer. I'm a Python newbie and have what I assume is a basic question. os.listdir takes a pathname as an arg...
7
by: Kenneth Pronovici | last post by:
I have some confusion regarding the relationship between locale, os.listdir() and unicode pathnames. I'm running Python 2.3.5 on a Debian system. If it matters, all of the files I'm dealing with...
15
by: Riccardo Galli | last post by:
Hi, I noticed that when I use os.listdir I need to work with absolute paths 90% of times. While I can use a for cycle, I'd prefere to use a list comprehension, but it becomes too long. I...
3
by: Jerome Cohen | last post by:
AI am trying to call a third-party web service. this service expects an XML fragment that contains the request plus other parameter. adding the web reference created the syntax below(reference.vb)....
3
by: vedrandekovic | last post by:
Hello Here is my simple listdir example: Here is my error: WindowsError: The system cannot find the path specified: 'l/ *.*'
0
by: scriptmann | last post by:
Hi, I'm trying to use os.listdir() to list directories with simplified chinese filenames. However, while I can see the filenames correctly in windows explorer, I am getting ? in the filename...
6
by: Soren | last post by:
Hi, I'd like to read the filenames in a directory, but not the subdirectories, os.listdir() gives me everything... how do I separate the directory names from the filenames? Is there another way...
6
by: jimgardener | last post by:
hi i have a directory containing .pgm files of P5 type.i wanted to read the pixel values of these files ,so as a firststep i wrote code to make a list of filenames using listdir ...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.