Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Windows os.listdir('') -> cwd and os.listdir(u'') -> C:\ #50163

Closed
ezio-melotti opened this issue May 3, 2009 · 9 comments
Closed

On Windows os.listdir('') -> cwd and os.listdir(u'') -> C:\ #50163

ezio-melotti opened this issue May 3, 2009 · 9 comments
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ezio-melotti
Copy link
Member

BPO 5913
Nosy @loewis, @pitrou, @ezio-melotti, @bitdancer
Files
  • listdir_on_empty_path.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2009-05-04.05:59:55.596>
    created_at = <Date 2009-05-03.13:44:59.874>
    labels = ['type-bug', 'library', 'OS-windows']
    title = "On Windows os.listdir('') -> cwd and os.listdir(u'') -> C:\\"
    updated_at = <Date 2009-05-04.06:21:53.091>
    user = 'https://github.com/ezio-melotti'

    bugs.python.org fields:

    activity = <Date 2009-05-04.06:21:53.091>
    actor = 'ezio.melotti'
    assignee = 'ocean-city'
    closed = True
    closed_date = <Date 2009-05-04.05:59:55.596>
    closer = 'ocean-city'
    components = ['Library (Lib)', 'Windows']
    creation = <Date 2009-05-03.13:44:59.874>
    creator = 'ezio.melotti'
    dependencies = []
    files = ['13861']
    hgrepos = []
    issue_num = 5913
    keywords = ['patch']
    message_count = 9.0
    messages = ['87045', '87054', '87056', '87080', '87094', '87095', '87097', '87099', '87101']
    nosy_count = 5.0
    nosy_names = ['loewis', 'pitrou', 'ocean-city', 'ezio.melotti', 'r.david.murray']
    pr_nums = []
    priority = 'high'
    resolution = 'fixed'
    stage = 'test needed'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue5913'
    versions = ['Python 3.1', 'Python 2.7']

    @ezio-melotti
    Copy link
    Member Author

    On Windows, with Python2/Python3, os.listdir('')/os.listdir(b'') list
    the content of the current working directory and
    os.listdir(u'')/os.listdir('') the content of C:\.

    On Linux the error "OSError: [Errno 2] No such file or directory: ''" is
    raised.

    I also noticed that os.listdir('C:') (without the \) lists the content
    of the cwd too.

    @ezio-melotti ezio-melotti added stdlib Python modules in the Lib dir OS-windows type-bug An unexpected behavior, bug, or error labels May 3, 2009
    @pitrou
    Copy link
    Member

    pitrou commented May 3, 2009

    As for the "C:" behaviour, I think it is normal: you are not specifying
    the path itself, only the drive letter, so it uses the current path in
    the specified drive (which /is/ cwd if you are already running from C:,
    but may be something else if running from another drive).

    As for '' and b'', I think that ambiguity is bad and these values should
    be prohibited by Python (ValueError comes to mind).

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented May 3, 2009

    Before anything is changed, I would first like to understand where the
    difference comes from.

    If it gives ENOENT on Unix, it should also give ENOENT on Windows
    (rather than giving ValueError). In addition, it should also give what
    open("")/open(b"") gives - as it really is the request to open the
    directory named "". (disclaimer: I haven done any tests or code review yet)

    @bitdancer
    Copy link
    Member

    On Sun, 3 May 2009 at 17:36, Antoine Pitrou wrote:

    As for the "C:" behaviour, I think it is normal: you are not specifying
    the path itself, only the drive letter, so it uses the current path in
    the specified drive (which /is/ cwd if you are already running from C:,
    but may be something else if running from another drive).

    It is normal behavior for Windows. Try it with the 'dir' command
    in a command window. (That said, I have no idea, not being a
    Windows user, how Windows decides what the 'current' directory
    is on any given drive from any given context).

    @ocean-city
    Copy link
    Mannequin

    ocean-city mannequin commented May 4, 2009

    I hope attached patch works.

    >>> import os
    >>> os.listdir("")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    WindowsError: [Error 3] 指定されたパスが見つかりません。: ''
    [36200 refs]
    >>> os.listdir(u"")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    WindowsError: [Error 3] 指定されたパスが見つかりません。: u''

    # Error message says "Cannot find specified path"

    @ocean-city
    Copy link
    Mannequin

    ocean-city mannequin commented May 4, 2009

    For u"", os.listdir calls FindFirstFileW with u"\\*.*",
    For "", os.listdir calls FindFirstFileA with "*.*".

    The code before FindFirstFile[AW] is slightly different for empty path.

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented May 4, 2009

    Hirokazu, the patch looks fine, please apply to 2.7 and 3k.

    I wouldn't backport it to 2.6/3.0, since it may break existing code.

    @loewis loewis mannequin assigned ocean-city May 4, 2009
    @ocean-city
    Copy link
    Mannequin

    ocean-city mannequin commented May 4, 2009

    Committed in r72273(trunk) and r72274(py3k).

    @ocean-city ocean-city mannequin closed this as completed May 4, 2009
    @ezio-melotti
    Copy link
    Member Author

    It is normal behavior for Windows. Try it with the 'dir' command
    in a command window. (That said, I have no idea, not being a
    Windows user, how Windows decides what the 'current' directory
    is on any given drive from any given context).

    Yes, I tried to run cmd.exe and I did:

    C:\> cd D:\foo
    C:\> cd E:\foo
    C:\> python

    os.listdir('D:') and os.listdir('E:') then listed the content of D:\foo
    and E:\bar. This is the correct behaviour, even if it's not documented.
    The CWD is the one where Python has been launched, and apparently on the
    other drives is the root folder by default, if it's not changed with the
    cd command.

    Thanks Hirokazu for the patch.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants