This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pathlib glob ignores trailing slash in pattern
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: emilyemorehouse Nosy List: andrei.avk, cvrebert, emilyemorehouse, joca.bt, pitrou, r.david.murray, robbuckley
Priority: normal Keywords: patch

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:07adminsetgithub: 66472
2021-11-21 02:47:05andrei.avksetmessages: + msg406702
2021-11-21 02:35:48andrei.avksetmessages: + msg406701
2021-11-21 02:34:22andrei.avksetnosy: + andrei.avk
messages: + msg406700
2018-11-06 01:58:31E Kawashimasetkeywords: + patch
stage: test needed -> patch review
pull_requests: + pull_request9649
2018-05-05 04:18:04SilentGhostsetversions: + Python 3.8, - Python 3.7
2018-05-05 04:17:38SilentGhostsetnosy: + robbuckley, - brianmsheldon
2018-05-05 04:16:55SilentGhostsetassignee: emilyemorehouse
stage: test needed

nosy: + emilyemorehouse, brianmsheldon
versions: + Python 3.7, - Python 3.4, Python 3.5
2018-05-05 04:14:00SilentGhostlinkissue33392 superseder
2014-08-29 23:07:43r.david.murraysetmessages: + msg226102
2014-08-29 22:31:16pitrousettitle: pathlib glob issues -> pathlib glob ignores trailing slash in pattern
2014-08-29 22:30:51pitrousetmessages: + msg226095
2014-08-29 03:26:46cvrebertsetnosy: + cvrebert
2014-08-28 17:58:30r.david.murraysetnosy: + r.david.murray
messages: + msg226036
2014-08-28 17:50:35joca.btsetmessages: + msg226034
2014-08-28 17:46:02pitrousetmessages: + msg226033
2014-08-26 16:39:06serhiy.storchakasetnosy: + pitrou

versions: + Python 3.5
2014-08-26 11:37:22joca.btcreate