1010
1111__all__ = ["glob" , "iglob" , "escape" ]
1212
13- def glob (pathname , * , root_dir = None , dir_fd = None , recursive = False ):
13+ def glob (pathname , * , root_dir = None , dir_fd = None , recursive = False ,
14+ include_hidden = False ):
1415 """Return a list of paths matching a pathname pattern.
1516
1617 The pattern may contain simple shell-style wildcards a la
17- fnmatch. However, unlike fnmatch, filenames starting with a
18+ fnmatch. Unlike fnmatch, filenames starting with a
1819 dot are special cases that are not matched by '*' and '?'
19- patterns.
20+ patterns by default .
2021
21- If recursive is true, the pattern '**' will match any files and
22+ If `include_hidden` is true, the patterns '*', '?', '**' will match hidden
23+ directories.
24+
25+ If `recursive` is true, the pattern '**' will match any files and
2226 zero or more directories and subdirectories.
2327 """
24- return list (iglob (pathname , root_dir = root_dir , dir_fd = dir_fd , recursive = recursive ))
28+ return list (iglob (pathname , root_dir = root_dir , dir_fd = dir_fd , recursive = recursive ,
29+ include_hidden = include_hidden ))
2530
26- def iglob (pathname , * , root_dir = None , dir_fd = None , recursive = False ):
31+ def iglob (pathname , * , root_dir = None , dir_fd = None , recursive = False ,
32+ include_hidden = False ):
2733 """Return an iterator which yields the paths matching a pathname pattern.
2834
2935 The pattern may contain simple shell-style wildcards a la
@@ -40,7 +46,8 @@ def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False):
4046 root_dir = os .fspath (root_dir )
4147 else :
4248 root_dir = pathname [:0 ]
43- it = _iglob (pathname , root_dir , dir_fd , recursive , False )
49+ it = _iglob (pathname , root_dir , dir_fd , recursive , False ,
50+ include_hidden = include_hidden )
4451 if not pathname or recursive and _isrecursive (pathname [:2 ]):
4552 try :
4653 s = next (it ) # skip empty string
@@ -50,7 +57,8 @@ def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False):
5057 pass
5158 return it
5259
53- def _iglob (pathname , root_dir , dir_fd , recursive , dironly ):
60+ def _iglob (pathname , root_dir , dir_fd , recursive , dironly ,
61+ include_hidden = False ):
5462 dirname , basename = os .path .split (pathname )
5563 if not has_magic (pathname ):
5664 assert not dironly
@@ -64,15 +72,18 @@ def _iglob(pathname, root_dir, dir_fd, recursive, dironly):
6472 return
6573 if not dirname :
6674 if recursive and _isrecursive (basename ):
67- yield from _glob2 (root_dir , basename , dir_fd , dironly )
75+ yield from _glob2 (root_dir , basename , dir_fd , dironly ,
76+ include_hidden = include_hidden )
6877 else :
69- yield from _glob1 (root_dir , basename , dir_fd , dironly )
78+ yield from _glob1 (root_dir , basename , dir_fd , dironly ,
79+ include_hidden = include_hidden )
7080 return
7181 # `os.path.split()` returns the argument itself as a dirname if it is a
7282 # drive or UNC path. Prevent an infinite recursion if a drive or UNC path
7383 # contains magic characters (i.e. r'\\?\C:').
7484 if dirname != pathname and has_magic (dirname ):
75- dirs = _iglob (dirname , root_dir , dir_fd , recursive , True )
85+ dirs = _iglob (dirname , root_dir , dir_fd , recursive , True ,
86+ include_hidden = include_hidden )
7687 else :
7788 dirs = [dirname ]
7889 if has_magic (basename ):
@@ -83,20 +94,21 @@ def _iglob(pathname, root_dir, dir_fd, recursive, dironly):
8394 else :
8495 glob_in_dir = _glob0
8596 for dirname in dirs :
86- for name in glob_in_dir (_join (root_dir , dirname ), basename , dir_fd , dironly ):
97+ for name in glob_in_dir (_join (root_dir , dirname ), basename , dir_fd , dironly ,
98+ include_hidden = include_hidden ):
8799 yield os .path .join (dirname , name )
88100
89101# These 2 helper functions non-recursively glob inside a literal directory.
90102# They return a list of basenames. _glob1 accepts a pattern while _glob0
91103# takes a literal basename (so it only has to check for its existence).
92104
93- def _glob1 (dirname , pattern , dir_fd , dironly ):
105+ def _glob1 (dirname , pattern , dir_fd , dironly , include_hidden = False ):
94106 names = _listdir (dirname , dir_fd , dironly )
95- if not _ishidden (pattern ):
96- names = (x for x in names if not _ishidden (x ))
107+ if include_hidden or not _ishidden (pattern ):
108+ names = (x for x in names if include_hidden or not _ishidden (x ))
97109 return fnmatch .filter (names , pattern )
98110
99- def _glob0 (dirname , basename , dir_fd , dironly ):
111+ def _glob0 (dirname , basename , dir_fd , dironly , include_hidden = False ):
100112 if basename :
101113 if _lexists (_join (dirname , basename ), dir_fd ):
102114 return [basename ]
@@ -118,10 +130,11 @@ def glob1(dirname, pattern):
118130# This helper function recursively yields relative pathnames inside a literal
119131# directory.
120132
121- def _glob2 (dirname , pattern , dir_fd , dironly ):
133+ def _glob2 (dirname , pattern , dir_fd , dironly , include_hidden = False ):
122134 assert _isrecursive (pattern )
123135 yield pattern [:0 ]
124- yield from _rlistdir (dirname , dir_fd , dironly )
136+ yield from _rlistdir (dirname , dir_fd , dironly ,
137+ include_hidden = include_hidden )
125138
126139# If dironly is false, yields all file names inside a directory.
127140# If dironly is true, yields only directory names.
@@ -164,13 +177,14 @@ def _listdir(dirname, dir_fd, dironly):
164177 return list (it )
165178
166179# Recursively yields relative pathnames inside a literal directory.
167- def _rlistdir (dirname , dir_fd , dironly ):
180+ def _rlistdir (dirname , dir_fd , dironly , include_hidden = False ):
168181 names = _listdir (dirname , dir_fd , dironly )
169182 for x in names :
170- if not _ishidden (x ):
183+ if include_hidden or not _ishidden (x ):
171184 yield x
172185 path = _join (dirname , x ) if dirname else x
173- for y in _rlistdir (path , dir_fd , dironly ):
186+ for y in _rlistdir (path , dir_fd , dironly ,
187+ include_hidden = include_hidden ):
174188 yield _join (x , y )
175189
176190
0 commit comments