You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pathlib glob ignores trailing slash in pattern #66472
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.)
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.
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.
Activity
jocabt commentedon Aug 26, 2014
Both fnmatch and glob support the "*/" glob. However, pathlib does not seem to handle this kind of globs correctly.
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.
pitrou commentedon Aug 28, 2014
What is "*/" supposed to do? Only select directories?
jocabt commentedon Aug 28, 2014
Yes.
bitdancer commentedon Aug 28, 2014
Heh. I never noticed that about shell globs, but it is logical. Learn something new every day.
pitrou commentedon 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.)
[-]pathlib glob issues[/-][+]pathlib glob ignores trailing slash in pattern[/+]bitdancer commentedon Aug 29, 2014
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 :)
akulakov commentedon Nov 21, 2021
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
lscommand 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
lsandglob.glob. I've usedlsin this manner before.akulakov commentedon Nov 21, 2021
I meant to say:
path.glob('myfile/') => [PosixPath('myfile')]
akulakov commentedon Nov 21, 2021
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/
barneygale commentedon Jul 22, 2022
This was fixed in #10349. This issue could be closed.
barneygale commentedon Jan 8, 2023
Fixed in #10349.