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('**') returns only directories #70303
The shell version of '*' and '**' return both directories and files.
Path('.').glob('*') returns both directories and files, but Path('.').glob('**') returns only directories. That seems wrong to me.
It may be what the documentation says it will do, but is not what it should do. I believe that because:
Currently ** in pathlib matches only directories, but **.py matches files. That seems inconsistent.
In bash, and csh, ** matches files and directories. To get the same in pathlib one must use **/*, which is inconsistent with what we have used for many decades.
With the traditional meaning of **, it is easy to constrain the match to directories by adding slash to the end of the glob (just use **/).
There is considerable value in supporting the traditional meaning of glob strings. Globbing is a very powerful feature, and it is often offered to the end user in shell-like situations. For example, sftp offers globbing. When offering globbing to the end users it is best to be consistent the globbing they are already familiar with.
There is no significant advantage to the difference between pathlib globbing and traditional globbing.
Globbing in pathlib is different from traditional globbing in another important way. pathlib does not distinguish between hidden files and directories and normal files and directories. There may be isolated cases where that is preferred, but generally that is not true. Indeed, the primary characteristic of being hidden is that it should not be included in globbing. One marks a file or directory to be hidden specifically to mean 'do not include this one when selecting groups of files or directories'. Once the glob string has been expanded, it is possible to filter out the hidden files and directories, but it very difficult to do if there are several levels of directories because when weeding out the matches that should not be be included you have to look for hidden items at all levels of the path.
Globbing has been available and largely unchanged for almost 50 years. I encourage you to strongly consider making pathlib globbing more consistent with what we have all grown up with.
Re-opening - this is a valid issue. As the previous poster notes, pathlib's ** is incompatible with every other glob implementation out there, including Python's own glob.glob():
If recursive is true, the pattern “**” will match any files and zero or more directories, subdirectories and symbolic links to directories. If the pattern is followed by an os.sep or os.altsep then files will not match.
It's easy enough to solve - just delete these lines:
The Path.glob says that patterns are the same as for fnmatch, except that ** is always recursive. The fnmatch doc says nothing about **. The glob dob says glob is essentially scandir+fnmatch, and that the behavior of ** depends on options selected.
If recursive is true, the pattern ** will match any files and zero or more directories, subdirectories and symbolic links to directories. If the pattern is followed by an os.sep or os.altsep then files will not match.
If include_hidden is true, ** pattern will match hidden directories.)
To me, the current Path.glob ** is a behavior that should be fixed.
Reacted by C.A.M. Gerlach, Barney Gale, Gregory P. Smith, Oleg Iarygin and Edgar Ramírez Mondragón
Activity
jitterman commentedon Jan 14, 2016
The title says it all.
The shell version of '*' and '**' return both directories and files.
Path('.').glob('*') returns both directories and files, but Path('.').glob('**') returns only directories. That seems wrong to me.
SilentGhost commentedon Jan 14, 2016
It is, however, exactly what documentation says it should do:
jitterman commentedon Feb 4, 2016
It may be what the documentation says it will do, but is not what it should do. I believe that because:
Globbing in pathlib is different from traditional globbing in another important way. pathlib does not distinguish between hidden files and directories and normal files and directories. There may be isolated cases where that is preferred, but generally that is not true. Indeed, the primary characteristic of being hidden is that it should not be included in globbing. One marks a file or directory to be hidden specifically to mean 'do not include this one when selecting groups of files or directories'. Once the glob string has been expanded, it is possible to filter out the hidden files and directories, but it very difficult to do if there are several levels of directories because when weeding out the matches that should not be be included you have to look for hidden items at all levels of the path.
Globbing has been available and largely unchanged for almost 50 years. I encourage you to strongly consider making pathlib globbing more consistent with what we have all grown up with.
barneygale commentedon May 29, 2023
Re-opening - this is a valid issue. As the previous poster notes, pathlib's
**is incompatible with every other glob implementation out there, including Python's ownglob.glob():It's easy enough to solve - just delete these lines:
cpython/Lib/pathlib.py
Lines 1046 to 1048 in 24af451
However we'll need to go through a deprecation period first.nope!pythonGH-70303: Emit FutureWarning when pathlib glob pattern ends wit…
terryjreedy commentedon Jun 17, 2023
The Path.glob says that patterns are the same as for fnmatch, except that
**is always recursive. The fnmatch doc says nothing about**. The glob dob says glob is essentially scandir+fnmatch, and that the behavior of**depends on options selected.To me, the current Path.glob
**is a behavior that should be fixed.GH-70303: Emit FutureWarning when pathlib glob pattern ends with `**` (…
pythonGH-70303: Make `pathlib.Path.glob('**')` return both files and …
GH-70303: Make `pathlib.Path.glob('**')` return both files and direct…
barneygale commentedon Jan 30, 2024
Fixed in 3.13 / fda7445 / #114684
Adjust tests for improved implementation of ** globs in Python 3.13+
Adjust tests for improved implementation of ** globs in Python 3.13+
pythonGH-70303: Make `pathlib.Path.glob('**')` return both files and …
wxgeo commentedon Jan 18, 2026
While this change of behaviour makes sense, the fact that the behavior changed in 3.13 should appear clearly in the doc:
https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob
Something like: