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

pathlib .glob('*/') returns files as well as directories #77573

Closed
robbuckley mannequin opened this issue Apr 30, 2018 · 5 comments
Closed

pathlib .glob('*/') returns files as well as directories #77573

robbuckley mannequin opened this issue Apr 30, 2018 · 5 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@robbuckley
Copy link
Mannequin

robbuckley mannequin commented Apr 30, 2018

BPO 33392
Nosy @emilyemorehouse, @robbuckley, @BrianMSheldon
PRs
  • bpo-22276: Fix pathlib.Path.glob not to ignore trailing path separator #10349
  • Superseder
  • bpo-22276: pathlib glob ignores trailing slash in pattern
  • 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 = 'https://github.com/emilyemorehouse'
    closed_at = <Date 2018-05-05.04:14:00.569>
    created_at = <Date 2018-04-30.13:17:33.404>
    labels = ['type-bug']
    title = "pathlib .glob('*/') returns files as well as directories"
    updated_at = <Date 2018-11-17.20:25:43.684>
    user = 'https://github.com/robbuckley'

    bugs.python.org fields:

    activity = <Date 2018-11-17.20:25:43.684>
    actor = 'E Kawashima'
    assignee = 'emilyemorehouse'
    closed = True
    closed_date = <Date 2018-05-05.04:14:00.569>
    closer = 'SilentGhost'
    components = []
    creation = <Date 2018-04-30.13:17:33.404>
    creator = 'robbuckley'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33392
    keywords = []
    message_count = 5.0
    messages = ['315949', '315950', '316007', '316009', '316192']
    nosy_count = 3.0
    nosy_names = ['emilyemorehouse', 'robbuckley', 'brianmsheldon']
    pr_nums = ['10349']
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '22276'
    type = 'behavior'
    url = 'https://bugs.python.org/issue33392'
    versions = ['Python 3.6']

    @robbuckley
    Copy link
    Mannequin Author

    robbuckley mannequin commented Apr 30, 2018

    Path.cwd().glob('/*') seems to yield all files and folders in cwd, the same as .glob('*').

    I believe that glob('*/') should yield only directories, and glob('*') all files and directories. this behaviour isnt documented one way or the other. But it would be consistent with glob.glob()

    console dump follows:

    Python 3.6.5 |Anaconda custom (64-bit)| (default, Mar 29 2018, 13:14:23)
    Type 'copyright', 'credits' or 'license' for more information
    IPython 6.3.1 -- An enhanced Interactive Python. Type '?' for help.

    In [1]: import os

    In [2]: from pathlib import Path

    In [6]: import glob

    In [13]: list(p.glob('*/'))
    Out[13]:
    [PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f1.txt'),
    PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f3.txt'),
    PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/d1'),
    PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f2.txt'),
    PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/d2')]

    In [14]: list(p.glob('*'))
    Out[14]:
    [PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f1.txt'),
    PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f3.txt'),
    PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/d1'),
    PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f2.txt'),
    PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/d2')]

    In [15]: glob.glob(os.path.join(str(p), '*/'))
    Out[15]:
    ['/Users/robertbuckley/python-scripts/pathlib_bug/d1/',
    '/Users/robertbuckley/python-scripts/pathlib_bug/d2/']

    In [16]: glob.glob(os.path.join(str(p), '*'))
    Out[16]:
    ['/Users/robertbuckley/python-scripts/pathlib_bug/f1.txt',
    '/Users/robertbuckley/python-scripts/pathlib_bug/f3.txt',
    '/Users/robertbuckley/python-scripts/pathlib_bug/d1',
    '/Users/robertbuckley/python-scripts/pathlib_bug/f2.txt',
    '/Users/robertbuckley/python-scripts/pathlib_bug/d2']

    @robbuckley robbuckley mannequin added the type-bug An unexpected behavior, bug, or error label Apr 30, 2018
    @robbuckley
    Copy link
    Mannequin Author

    robbuckley mannequin commented Apr 30, 2018

    this is workaroundable via constructions like:

    list(x for x in p.glob('*/') if x.is_dir())

    but I think there's value in behaving like glob.glob(), and keystroke avoidance for what must be a fairly common use case

    @emilyemorehouse
    Copy link
    Member

    emilyemorehouse commented May 1, 2018

    Good find -- I agree that when using Path.cwd().glob('*/'), it should only return directories. This follows the original glob library's functionality as well as the Unix expectation (I believe Windows as well, but I'll double check).

    I'll work on a fix!

    @emilyemorehouse emilyemorehouse self-assigned this May 1, 2018
    @robbuckley
    Copy link
    Mannequin Author

    robbuckley mannequin commented May 1, 2018

    I checked this on Mac and Windows, pathlib and the old glob std lib behave
    the same on both in this respect

    On Tue, 1 May 2018, 18:35 Emily Morehouse, <report@bugs.python.org> wrote:

    Emily Morehouse <emily@cuttlesoft.com> added the comment:

    Good find -- I agree that when using Path.cwd().glob('*/'), it should only
    return directories. This follows the original glob library's functionality
    as well as the Unix expectation (I believe Windows as well, but I'll double
    check).

    I'll work on a fix!

    ----------
    assignee: -> emilyemorehouse
    nosy: +emilyemorehouse


    Python tracker <report@bugs.python.org>
    <https://bugs.python.org/issue33392\>


    @BrianMSheldon
    Copy link
    Mannequin

    BrianMSheldon mannequin commented May 5, 2018

    This appears to be a duplicate of bpo-22276

    @SilentGhost SilentGhost mannequin closed this as completed May 5, 2018
    @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
    type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant