diff --git a/Lib/glob.py b/Lib/glob.py --- a/Lib/glob.py +++ b/Lib/glob.py @@ -77,7 +77,8 @@ # takes a literal basename (so it only has to check for its existence). def _glob1(dirname, pattern, dironly): - names = list(_iterdir(dirname, dironly)) + names = list(x.name for x in _iterdir(dirname, dironly) + if not dironly or x.is_dir()) if not _ishidden(pattern): names = (x for x in names if not _ishidden(x)) return fnmatch.filter(names, pattern) @@ -88,6 +89,9 @@ # directory separator. 'q*x/' should match only directories. if os.path.isdir(dirname): return [basename] + elif dironly: + if os.path.isdir(os.path.join(dirname, basename)): + return [basename] else: if os.path.lexists(os.path.join(dirname, basename)): return [basename] @@ -115,7 +119,7 @@ yield pattern[:0] yield from _rlistdir(dirname, dironly) -# If dironly is flase, yields all file names inside a directory. +# If dironly is false, yields all file names inside a directory. # If dironly is true, yields only directory or symlink (that can link to # a directory) names. def _iterdir(dirname, dironly): @@ -127,8 +131,8 @@ try: for entry in os.scandir(dirname): try: - if not dironly or entry.is_dir() or entry.is_symlink(): - yield entry.name + if not dironly or entry.is_dir(): + yield entry except OSError: pass except OSError: @@ -136,13 +140,18 @@ # Recursively yields relative pathnames inside a literal directory. def _rlistdir(dirname, dironly): - names = list(_iterdir(dirname, dironly)) - for x in names: - if not _ishidden(x): - yield x - path = os.path.join(dirname, x) if dirname else x - for y in _rlistdir(path, dironly): - yield os.path.join(x, y) + entries = list(_iterdir(dirname, dironly)) + for x in entries: + name = x.name + if not _ishidden(name): + yield name + try: + isdir = x.is_dir() + except OSError: + continue + if isdir: + for y in _rlistdir(x.path, dironly): + yield os.path.join(name, y) magic_check = re.compile('([*?[])')