Skip to content

pathlib glob ignores trailing slash in pattern #66472

Closed

Description

@jocabt
mannequin
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']

    Activity

    1. jocabt commented on Aug 26, 2014

      jocabtmannequin
      MannequinAuthor

      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.

    2. added
      stdlib
      type-bug
      on Aug 26, 2014
    3. pitrou commented on Aug 28, 2014

      @pitrou
      Member

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

    4. jocabt commented on Aug 28, 2014

      jocabtmannequin
      MannequinAuthor

      Yes.

    5. bitdancer commented on Aug 28, 2014

      @bitdancer
      Member

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

    6. pitrou commented on Aug 29, 2014

      @pitrou
      Member

      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.)

    7. changed the title [-]pathlib glob issues[/-] [+]pathlib glob ignores trailing slash in pattern[/+] on Aug 29, 2014
    8. bitdancer commented on Aug 29, 2014

      @bitdancer
      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 :)

    9. added
      3.8 (EOL)
      and removed
      3.7 (EOL)
      on May 5, 2018
    10. akulakov commented on Nov 21, 2021

      @akulakov
      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.

    11. akulakov commented on Nov 21, 2021

      @akulakov
      Contributor

      I meant to say:

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

    12. akulakov commented on Nov 21, 2021

      @akulakov
      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/

    13. transferred this issue fromon Apr 10, 2022
    14. barneygale commented on Jul 22, 2022

      @barneygale
      Contributor

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

    15. barneygale commented on Jan 8, 2023

      @barneygale
      Contributor

      Fixed in #10349.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    3.8 (EOL)stdlibtopic-pathlibtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions