Skip to content

Commit

Permalink
migrate os.listdir() to os.scandir() to increase performance
Browse files Browse the repository at this point in the history
As recommended in https://peps.python.org/pep-0471/

Signed-off-by: Miroslav Suchý <msuchy@redhat.com>
  • Loading branch information
xsuchy committed Jan 17, 2025
1 parent 395b971 commit a6c4f96
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/commoncode/fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,6 @@ def walk(location, ignored=None, follow_symlinks=False):
If `follow_symlinks` is True, then symlinks will not be ignored and be
collected like regular files and directories
"""
# TODO: consider using the new "scandir" module for some speed-up.

is_ignored = ignored(location) if ignored else False
if is_ignored:
if TRACE:
Expand All @@ -335,13 +333,12 @@ def walk(location, ignored=None, follow_symlinks=False):
elif filetype.is_dir(location, follow_symlinks=follow_symlinks):
dirs = []
files = []
# TODO: consider using scandir
for name in os.listdir(location):
loc = os.path.join(location, name)
for entry in os.scandir(location):
loc = os.path.join(location, entry.name)
if filetype.is_special(loc) or (ignored and ignored(loc)):
if (
follow_symlinks
and filetype.is_link(loc)
and entry.is_symlink()
and not filetype.is_broken_link(location)
):
pass
Expand All @@ -351,10 +348,10 @@ def walk(location, ignored=None, follow_symlinks=False):
logger_debug("walk: ignored:", loc, ign)
continue
# special files and symlinks are always ignored
if filetype.is_dir(loc, follow_symlinks=follow_symlinks):
dirs.append(name)
elif filetype.is_file(loc, follow_symlinks=follow_symlinks):
files.append(name)
if entry.is_dir(follow_symlinks=follow_symlinks):
dirs.append(entry.name)
elif entry.is_file(follow_symlinks=follow_symlinks):
files.append(entry.name)
yield location, dirs, files

for dr in dirs:
Expand Down

0 comments on commit a6c4f96

Please sign in to comment.