Closed
Description
the [Python] docs for
pathlib.globare unclear on the meaning of**
You are right, and it gets even more confusing:
glob.glob('**')returns dirs and files, non-recursively, not including.glob.glob('**', recursive=True)returns dirs and files, recursively, not including.list(pathlib.Path('.').glob('**'))returns only dirs, recursively, and also includes.
I think there should be a Python upstream issue to document all of that, including the differences between glob and pathlib.
Then zipp could implement what's documented for pathlib.
Repro
Python 3.10.11
mkdir -p dir/subdir ; touch toplevel.txt dir/indir.txtcreates:
toplevel.txt
dir/
indir.txt
subdir/
>>> import glob, pathlib
>>> glob.glob('**')
['dir', 'toplevel.txt']
>>> glob.glob('**', recursive=True)
['dir', 'dir/subdir', 'dir/indir.txt', 'toplevel.txt']
>>> list(pathlib.Path('.').glob('**'))
[PosixPath('.'), PosixPath('dir'), PosixPath('dir/subdir')]Originally posted by @nh2 in jaraco/zipp#102 (comment)
Linked PRs
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
- StatusShow more project fieldsDone
Activity
jaraco commentedon Jul 14, 2023
Without having thought about it too much, I'd argue
pathlib.Path.glob('**')should return dirs and files recursively, not including., same asglob.glob(recursive=True). I'd also argue thatglob.glob('**')should implyrecursive=True. After all, what's the meaning of**if not recursive?jaraco commentedon Jul 14, 2023
@barneygale Do you have any insight on the concern? Any opinions?
jaraco commentedon Jul 14, 2023
Another point of reference,
lson Unix also treats**as recursive without the dot:barneygale commentedon Jul 14, 2023
Yeah, it's a mess
Logged as #101112, stale-ish PR up at #101223
Try
glob.glob('tests/**', recursive=True)- you'll find thattestsis included in the results, and so**is capable of consuming zero path segments. The fact that it doesn't emit.seems like the bug to me! Also conflicts with the docs.Logged as #70303, PR up as #105413.
jaraco commentedon Jul 15, 2023
I'm going to be nitpicky because this issue demands it. Understand my matter of fact tone below may come off as hostile, but it's not intended that way. I'm merely trying to lay out the facts and disentangle the concerns.
More precisely,
tests/is included in the results:Note I'm using xonsh shell, which is why you see python interleaved with a shell prompt in the cpython repo.
Interestingly,
tests/is not included in the results ifrecursive=False.That's the opposite of my intuition. It's almost never the case that a user wants
.in their globs (any more than they want..). When I doglob('mydir/*'), I want to find all of the contents of "mydir", and "mydir" itself is not contained in "mydir".Even pathlib only emits the
.when a recursive glob is indicated:I'm now confused. These two statements seem contradictory:
In the case of
glob.glob('tests/**', recursive=True),tests/is emitted and that's equivalent totests/.. Are you saying you think it's a bug thattests/is emitted without a dot?Also, I'm unsure how it conflicts with the docs. Can you elaborate on where the conflict lies?
barneygale commentedon Jul 15, 2023
I was talking about
**, not*. The behaviour of standalone*pattern segments is well-defined: it always consumes one path segment.I'm saying that these two function calls should return the same result, as they do in pathlib:
**is described as matching "any files and zero or more directories". You can see that, in thetestdir/**example above, it can consume zero files and zero directories. But in the**example, it doesn't consume the current directory.barneygale commentedon Jul 15, 2023
FWIW, there is no single standard for how
**should work. Different shells and programming languages implement it differently. See https://stackoverflow.com/questions/70613723/recursive-globs-or-globstar-different-on-zsh-bash-python-and-rubybarneygale commentedon Jul 26, 2023
FYI, I'm looking to add a new sep argument to
fnmatch.translate()which would unify its pattern-matching withpathlib.PurePath.match()andpathlib.Path.glob(). In doing so it would makefnmatch.translate()more compatible withglob.glob(), but there would still be differences, such as the above-noted difference in how**works, but also the default treatment of hidden files/directories. PR here:glob.translate()function #106703jaraco commentedon Aug 4, 2023
I'm very interested in reviewing the proposed change. I'm particularly interested to see if it obviates or conflicts with zipp.glob.
15 remaining items
pythonGH-106747: Prepare pathlib globbing for dir_fd support.
serhiy-storchaka commentedon Mar 5, 2024
Let resolve some misunderstandings.
**withoutrecursive=Trueis interpreted as*(two subsequent*s, but this is the same). This is not an example for**.lsdoes not interpolate patterns. The shell does this. By default**is equivalent to*, which is expanded todir toplevel.txt.ls dirproducesindir.txt subdir, andls toplevel.txtproducestoplevel.txt. I do not know how did you get your result, except if you use very non-standard shell andls.The patch cannot have less than one component.
No file or directory name matches pattern "", therefore the
glob.glob()result is correct. As forpath.glob("")it is a matter of question how much we want it to differ fromglob.glob(). They cannot be completely equivalent for objective reasons, so we should decide what differences are deliberate. Raising an error is safer than producing a different result.barneygale commentedon Mar 5, 2024
Thanks for the clarifications
pathlib.Path('.')is a zero-component path in pathlib. Its.partsis an empty tuple, whereasPath('foo').partsis a 1-element tuple.os.path.relpath()needs to count components in order to generate the correct number of../components, and counts empty/dot paths as having zero components:barneygale commentedon Mar 5, 2024
FWIW, most of these differences highlighted in this thread are now either resolved or documented.
I'll open a PR to document that
glob.glob(root_dir=root_dir)never returns an empty path (that could represent theroot_dir), whereaspathlib.Path(root_dir).glob()can return theroot_dir. I think that'll cover the remaining differences.pythonGH-106747: Document another difference between `glob` and `path…
Merge branch 'main' into pythongh-106747-empty-path
GH-106747: Document another difference between `glob` and `pathlib`. (#…
pythonGH-106747: Document another difference between `glob` and `path…
Squashed commit of the following:
Squashed commit of the following:
pythonGH-106747: Document another difference between `glob` and `path…
pythonGH-106747: Improve `Path.glob()` expectations in pathlib tests (p…