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

Search Discussions

  • Gerrit Holl at Oct 4, 2003 at 3:44 pm
    <quote name="Hannu Kankaanp??" date="1065252981" email="hanzspam at 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 at 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
  • Terry Reedy at Oct 4, 2003 at 9:30 pm
    "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'.

    Terry J. Reedy
  • Bengt Richter at Oct 5, 2003 at 12:25 am

    On Sat, 4 Oct 2003 17:30:37 -0400, "Terry Reedy" 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
  • Hannu Kankaanp?? at Oct 5, 2003 at 10:57 am
    "Terry Reedy" <tjreedy at udel.edu> wrote in message news:<NK-dnYuNtPlipuKiU-KYuA at 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).
  • John Roth at Oct 5, 2003 at 11:29 am
    "Terry Reedy" <tjreedy at udel.edu> wrote in message
    news:NK-dnYuNtPlipuKiU-KYuA at comcast.com...
    "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'.
    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
  • Terry Reedy at Oct 5, 2003 at 4:05 pm
    "John Roth" <newsgroups at jhrothjr.com> wrote in message
    news:vo004qejvqfcaa at news.supernews.com...
    "Terry Reedy" <tjreedy at 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
  • Bengt Richter at Oct 5, 2003 at 7:50 pm

    On Sun, 5 Oct 2003 07:29:34 -0400, "John Roth" wrote:
    "Terry Reedy" <tjreedy at udel.edu> wrote in message
    news:NK-dnYuNtPlipuKiU-KYuA at comcast.com...
    "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'.
    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
  • Bernhard Herzog at Oct 6, 2003 at 1:23 pm

    bokr at oz.net (Bengt Richter) writes:
    On Sun, 5 Oct 2003 07:29:34 -0400, "John Roth" 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
  • Bengt Richter at Oct 7, 2003 at 5:04 am

    On Mon, 06 Oct 2003 15:23:42 +0200, Bernhard Herzog wrote:
    bokr at oz.net (Bengt Richter) writes:
    On Sun, 5 Oct 2003 07:29:34 -0400, "John Roth" 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.
    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
    os.listdir("")
    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


    From bogus@does.not.exist.com Tue Oct 7 07:06:43 2003
    From: bogus@does.not.exist.com (Rob)
    Date: Tue, 07 Oct 2003 01:06:43 -0400
    Subject: Python on Win32 and MFC
    References: <blkolg$ksv$1@biggoron.nerim.net>
    Message-ID: <8di4ovcaphtg054hk64e3f1ck5eoua3f6c@4ax.com>

    you might want to look at venster. It lighthly wraps the windows c
    functions in python.

    Rob
    On Fri, 3 Oct 2003 23:25:31 +0200, "Gilles Lenfant"
    wrote:
    Hi,

    I bought and read most of (excellent) Mark Hammond's book, Python
    programming on Win32.
    I started to make some sophisticated "hello world" app using win32ui (...)
    but I need some more information to make the bridge between this book and
    the MFC (black hole for me) in depth.

    Any advise on good reading, and some python examples ?

    Many thanks in advance.

    --Gilles


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
  • Bernhard Herzog at Oct 7, 2003 at 12:53 pm

    bokr at oz.net (Bengt Richter) writes:
    On Mon, 06 Oct 2003 15:23:42 +0200, Bernhard Herzog wrote:


    bokr at oz.net (Bengt Richter) writes:
    That intuitively matches well with arg-less invocation of ls or dir.
    -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 ;-)
    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedOct 4, '03 at 3:36p
activeOct 7, '03 at 12:53p
posts11
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase