473,785 Members | 3,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2450
<quote name="Hannu Kankaanp??" date="106525298 1" 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:gerr it@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.goog le.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'.

Terry J. Reedy
Jul 18 '05 #3
On Sat, 4 Oct 2003 17:30:37 -0400, "Terry Reedy" <tj*****@udel.e du> wrote:

"Hannu Kankaanp??" <ha******@yahoo .com.au> wrote in message
news:84******* *************** ***@posting.goo gle.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\hom e\bokr\test>pyt hon
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_inf o (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.e du> wrote in message news:<NK******* *************@c omcast.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.e du> wrote in message
news:NK******** ************@co mcast.com...

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

Jul 18 '05 #6

"John Roth" <ne********@jhr othjr.com> wrote in message
news:vo******** ****@news.super news.com...

"Terry Reedy" <tj*****@udel.e du> 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********@jhr othjr.com> wrote:

"Terry Reedy" <tj*****@udel.e du> wrote in message
news:NK******* *************@c omcast.com...

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

+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********@jhr othjr.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
23037
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 but it doesn't actually list the contents of the dir I pass in. it always operates on the current dir (wherever the script is run) and I have to chdir beforehand. Is that how its supposed to work? If so what is the point in passing in a...
7
3875
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 are on an ext3 filesystem. The real code this problem comes from takes a configured set of directories to deal with and walks through each of those directories using os.listdir(). Today, I accidentally ran across a directory containing three...
15
2581
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 propose to add an 'abs' keyword which would make os.listdir return the absolute path of files instead of a relative path. This would bring only advantages, I think.
3
5075
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). I changed the data type for the structure that contains the XML data from the default "String" to "xml.xmldocument" to enable easy filling of the data. my client code creates an XML document class, fills the data using standard xml dom...
3
8936
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
1825
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 strings returned by os.listdir() for some chinese characters. What should I do to make os.listdir return what I see in Windows Explorer (i.e. the full simplified chinese filename)? I am confused because os.listdir() is supposed to have unicode...
6
5690
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 of doing this? Thanks!
6
3762
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 pgmdir="f:\code\python\pgmgallery" # where i have pgm files g2=listdir(pgmdir) i get the following error WindowsError: The filename, directory name, or volume
7
2688
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 name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.