Issue 22276: pathlib glob ignores trailing slash in pattern (original) (raw)

Created on 2014-08-26 11:37 by joca.bt, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 10349 open E Kawashima,2018-11-06 01:58
Messages (9)
msg225914 - (view) Author: João Guerra (joca.bt) Date: 2014-08-26 11:37
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 Issue 21039 may be related to this.
msg226033 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-08-28 17:46
What is "*/" supposed to do? Only select directories?
msg226034 - (view) Author: João Guerra (joca.bt) Date: 2014-08-28 17:50
Yes.
msg226036 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-08-28 17:58
Heh. I never noticed that about shell globs, but it is logical. Learn something new every day.
msg226095 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-08-29 22:30
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.)
msg226102 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-08-29 23:07
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 :)
msg406700 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-11-21 02:34
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.
msg406701 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-11-21 02:35
I meant to say: path.glob('myfile/') => [PosixPath('myfile')]
msg406702 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-11-21 02:47
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/
History
Date User Action Args
2022-04-11 14:58:07 admin set github: 66472
2021-11-21 02:47:05 andrei.avk set messages: +
2021-11-21 02:35:48 andrei.avk set messages: +
2021-11-21 02:34:22 andrei.avk set nosy: + andrei.avkmessages: +
2018-11-06 01:58:31 E Kawashima set keywords: + patchstage: test needed -> patch reviewpull_requests: + <pull%5Frequest9649>
2018-05-05 04🔞04 SilentGhost set versions: + Python 3.8, - Python 3.7
2018-05-05 04:17:38 SilentGhost set nosy: + robbuckley, - brianmsheldon
2018-05-05 04:16:55 SilentGhost set assignee: emilyemorehousestage: test needednosy: + emilyemorehouse, brianmsheldonversions: + Python 3.7, - Python 3.4, Python 3.5
2018-05-05 04:14:00 SilentGhost link issue33392 superseder
2014-08-29 23:07:43 r.david.murray set messages: +
2014-08-29 22:31:16 pitrou set title: pathlib glob issues -> pathlib glob ignores trailing slash in pattern
2014-08-29 22:30:51 pitrou set messages: +
2014-08-29 03:26:46 cvrebert set nosy: + cvrebert
2014-08-28 17:58:30 r.david.murray set nosy: + r.david.murraymessages: +
2014-08-28 17:50:35 joca.bt set messages: +
2014-08-28 17:46:02 pitrou set messages: +
2014-08-26 16:39:06 serhiy.storchaka set nosy: + pitrouversions: + Python 3.5
2014-08-26 11:37:22 joca.bt create