FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

migrate os.listdir() to os.scandir() to increase performance by xsuchy · Pull Request #78 · aboutcode-org/commoncode · GitHub

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
21 changes: 7 additions & 14 deletions src/commoncode/fileutils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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,26 +333,21 @@ 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 not filetype.is_broken_link(location)
):
if follow_symlinks and entry.is_symlink() and not filetype.is_broken_link(location):
pass
else:
if TRACE:
ign = ignored and ignored(loc)
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

Back | FazBrowse Home | New Git URL