-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
Comments
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. |
What is "*/" supposed to do? Only select directories? |
Yes. |
Heh. I never noticed that about shell globs, but it is logical. Learn something new every day. |
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. |
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 ls myfile ls myfile/ I think many users will expect behavior consistent with |
I meant to say: path.glob('myfile/') => [PosixPath('myfile')] |
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/ |
This was fixed in #10349. This issue could be closed. |
Fixed in #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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: