Skip to content

Commit ca9c7e6

Browse files
Merge pull request #83 from aboutcode-org/scandir
Migrate remaining os.listdir to os.scandir
2 parents 63b1ebe + 1963f81 commit ca9c7e6

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

src/commoncode/filetype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def counter(location, counting_function):
233233
return count_fun(location)
234234
elif is_dir(location):
235235
count += sum(
236-
counter(os.path.join(location, p), counting_function) for p in os.listdir(location)
236+
counter(os.path.join(location, p.name), counting_function) for p in os.scandir(location)
237237
)
238238
return count
239239

src/commoncode/fileutils.py

Lines changed: 9 additions & 12 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,13 +333,12 @@ 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 resource in os.scandir(location):
337+
loc = os.path.join(location, resource.name)
341338
if filetype.is_special(loc) or (ignored and ignored(loc)):
342339
if (
343340
follow_symlinks
344-
and filetype.is_link(loc)
341+
and resource.is_symlink()
345342
and not filetype.is_broken_link(location)
346343
):
347344
pass
@@ -351,10 +348,10 @@ def walk(location, ignored=None, follow_symlinks=False):
351348
logger_debug("walk: ignored:", loc, ign)
352349
continue
353350
# 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)
351+
if resource.is_dir(follow_symlinks=follow_symlinks):
352+
dirs.append(resource.name)
353+
elif resource.is_file(follow_symlinks=follow_symlinks):
354+
files.append(resource.name)
358355
yield location, dirs, files
359356

360357
for dr in dirs:
@@ -403,7 +400,7 @@ def copytree(src, dst):
403400
if not filetype.is_readable(src):
404401
chmod(src, R, recurse=False)
405402

406-
names = os.listdir(src)
403+
names = [resource.name for resource in os.scandir(src)]
407404

408405
if not os.path.exists(dst):
409406
os.makedirs(dst)
@@ -544,7 +541,7 @@ def _rm_handler(function, path, excinfo): # NOQA
544541
"""
545542
if TRACE:
546543
logger_debug("_rm_handler:", "path:", path, "excinfo:", excinfo)
547-
if function in (os.rmdir, os.listdir):
544+
if function in (os.rmdir, os.listdir, os.scandir):
548545
try:
549546
chmod(path, RW, recurse=True)
550547
shutil.rmtree(path, True)

src/commoncode/resource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def __init__(
292292
self.is_file = filetype_is_file(location)
293293

294294
# True if this codebase root is a file or an empty directory.
295-
self.has_single_resource = bool(self.is_file or not os.listdir(location))
295+
self.has_single_resource = bool(self.is_file or not os.scandir(location))
296296

297297
########################################################################
298298
# Set up caching, summary, timing, and error info

tests/test_fileutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_copyfile_does_not_keep_permissions(self):
168168
assert not filetype.is_readable(src_file)
169169

170170
fileutils.copyfile(src_file, dest)
171-
dest_file = join(dest, os.listdir(dest)[0])
171+
dest_file = join(dest, list(os.scandir(dest))[0].name)
172172
assert filetype.is_readable(dest_file)
173173
finally:
174174
fileutils.chmod(src_file, fileutils.RW, recurse=True)

0 commit comments

Comments
 (0)