Skip to content

Commit 8fbe7f2

Browse files
committed
migrate os.listdir() to os.scandir() to increase performance
As recommended in https://peps.python.org/pep-0471/ Signed-off-by: Miroslav Suchý <[email protected]>
1 parent 395b971 commit 8fbe7f2

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/commoncode/fileutils.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,6 @@ def walk(location, ignored=None, follow_symlinks=False):
321321
If `follow_symlinks` is True, then symlinks will not be ignored and be
322322
collected like regular files and directories
323323
"""
324-
# TODO: consider using the new "scandir" module for some speed-up.
325-
326324
is_ignored = ignored(location) if ignored else False
327325
if is_ignored:
328326
if TRACE:
@@ -335,26 +333,21 @@ def walk(location, ignored=None, follow_symlinks=False):
335333
elif filetype.is_dir(location, follow_symlinks=follow_symlinks):
336334
dirs = []
337335
files = []
338-
# TODO: consider using scandir
339-
for name in os.listdir(location):
340-
loc = os.path.join(location, name)
336+
for entry in os.scandir(location):
337+
loc = os.path.join(location, entry.name)
341338
if filetype.is_special(loc) or (ignored and ignored(loc)):
342-
if (
343-
follow_symlinks
344-
and filetype.is_link(loc)
345-
and not filetype.is_broken_link(location)
346-
):
339+
if follow_symlinks and entry.is_symlink() and not filetype.is_broken_link(location):
347340
pass
348341
else:
349342
if TRACE:
350343
ign = ignored and ignored(loc)
351344
logger_debug("walk: ignored:", loc, ign)
352345
continue
353346
# special files and symlinks are always ignored
354-
if filetype.is_dir(loc, follow_symlinks=follow_symlinks):
355-
dirs.append(name)
356-
elif filetype.is_file(loc, follow_symlinks=follow_symlinks):
357-
files.append(name)
347+
if entry.is_dir(follow_symlinks=follow_symlinks):
348+
dirs.append(entry.name)
349+
elif entry.is_file(follow_symlinks=follow_symlinks):
350+
files.append(entry.name)
358351
yield location, dirs, files
359352

360353
for dr in dirs:

0 commit comments

Comments
 (0)