pathlib glob ignores trailing slash in pattern #66472
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
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'
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']
The text was updated successfully, but these errors were encountered:
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.
bpo-21039 may be related to this.
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 :)
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
Path('myfile/').exists() => True
Path('myfile/') == Path('myfile') => True
str(Path('myfile/')) => 'myfile'
You can compare this to behavior of
I think many users will expect behavior consistent with
Generally if Path is created with a trailing separator, I think it should error out for all methods that apply to files, for example
This is consistent with shell commands:
touch xyz/ touch: xyz/: Not a directory
echo 'blah' > xyz/