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 ignores trailing slash in pattern #66472

Closed
jocabt mannequin opened this issue Aug 26, 2014 · 11 comments
Closed

pathlib glob ignores trailing slash in pattern #66472

jocabt mannequin opened this issue Aug 26, 2014 · 11 comments
Assignees
Labels
3.8 only security fixes stdlib Python modules in the Lib dir topic-pathlib type-bug An unexpected behavior, bug, or error

Comments

@jocabt
Copy link
Mannequin

jocabt mannequin commented Aug 26, 2014

BPO 22276
Nosy @pitrou, @bitdancer, @emilyemorehouse, @robbuckley, @akulakov
PRs
  • bpo-22276: Fix pathlib.Path.glob not to ignore trailing path separator #10349
  • 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 = None
    created_at = <Date 2014-08-26.11:37:22.662>
    labels = ['3.8', 'type-bug', 'library']
    title = 'pathlib glob ignores trailing slash in pattern'
    updated_at = <Date 2021-11-21.02:47:05.959>
    user = 'https://bugs.python.org/jocabt'

    bugs.python.org fields:

    activity = <Date 2021-11-21.02:47:05.959>
    actor = 'andrei.avk'
    assignee = 'emilyemorehouse'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2014-08-26.11:37:22.662>
    creator = 'joca.bt'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 22276
    keywords = ['patch']
    message_count = 9.0
    messages = ['225914', '226033', '226034', '226036', '226095', '226102', '406700', '406701', '406702']
    nosy_count = 7.0
    nosy_names = ['pitrou', 'r.david.murray', 'cvrebert', 'joca.bt', 'emilyemorehouse', 'robbuckley', 'andrei.avk']
    pr_nums = ['10349']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue22276'
    versions = ['Python 3.8']

    @jocabt
    Copy link
    Mannequin Author

    jocabt mannequin commented Aug 26, 2014

    Both fnmatch and glob support the "*/" glob. However, pathlib does not seem to handle this kind of globs correctly.

    dir = Path("/a/directory/")
    file = Path("/a/file")
    print(dir.match("*/")) # True
    print(file.match("*/")) # True

    The "/" is being discarded by the match, resulting in incorrect matches.

    Both the fnmatch and glob libraries support this correct.
    print(fnmatch("/a/directory/", "*/")) # True
    print(fnmatch("/a/file", "*/")) # False

    bpo-21039 may be related to this.

    @jocabt jocabt mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 26, 2014
    @pitrou
    Copy link
    Member

    pitrou commented Aug 28, 2014

    What is "*/" supposed to do? Only select directories?

    @jocabt
    Copy link
    Mannequin Author

    jocabt mannequin commented Aug 28, 2014

    Yes.

    @bitdancer
    Copy link
    Member

    Heh. I never noticed that about shell globs, but it is logical. Learn something new every day.

    @pitrou
    Copy link
    Member

    pitrou commented Aug 29, 2014

    Well, it would be logical if pathlib gave special meaning to trailing slashes, which it (still) doesn't :-) I'm still not fond of encoding path characteristics in the path string itself.
    (and what about symlinks? etc.)

    @pitrou pitrou changed the title pathlib glob issues pathlib glob ignores trailing slash in pattern Aug 29, 2014
    @bitdancer
    Copy link
    Member

    I'm not sure that a trailing '/' is a "path characteristic" in the same sense that a symlink is. Whether the path has a trailing slash or not has meaning both to the user and to the OS. pathlib isn't just modeling actual path objects on the file system, but the abstract concept of a path, and in the abstract context the presence or absence of a trailing '/' as meaning.

    But that's a wider discussion than this issue :)

    @SilentGhost SilentGhost mannequin added the 3.7 (EOL) end of life label May 5, 2018
    @SilentGhost SilentGhost mannequin added 3.8 only security fixes and removed 3.7 (EOL) end of life labels May 5, 2018
    @akulakov
    Copy link
    Contributor

    I have also run into this when looking into path.glob('dangling_symlink') issue.

    I can add a few things (in the examples, *myfile* is a file, not a directory):

    This is probably more common / less obscure than '*/':

    path.glob('myfile/') => True

    This is inconsistent with how shell ls command works and with glob.glob() and looks wrong.

    Path('myfile/').exists() => True

    Path('myfile/') == Path('myfile') => True

    str(Path('myfile/')) => 'myfile'

    You can compare this to behavior of ls (tested on MacOS):

    ls myfile
    myfile

    ls myfile/
    ls: myfile/: Not a directory

    I think many users will expect behavior consistent with ls and glob.glob. I've used ls in this manner before.

    @akulakov
    Copy link
    Contributor

    I meant to say:

    path.glob('myfile/') => [PosixPath('myfile')]

    @akulakov
    Copy link
    Contributor

    Generally if Path is created with a trailing separator, I think it should error out for all methods that apply to files, for example .touch(), read*(), write*(), others.

    This is consistent with shell commands:

    touch xyz/ touch: xyz/: Not a directory

    echo 'blah' > xyz/
    zsh: not a directory: xyz/

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @barneygale
    Copy link
    Contributor

    This was fixed in #10349. This issue could be closed.

    @barneygale
    Copy link
    Contributor

    Fixed in #10349.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir topic-pathlib type-bug An unexpected behavior, bug, or error
    Projects
    Development

    No branches or pull requests

    6 participants