Skip to content

Commit 1963f81

Browse files
Migrate remaining os.listdir to os.scandir
Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent de159a4 commit 1963f81

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
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: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -333,21 +333,25 @@ def walk(location, ignored=None, follow_symlinks=False):
333333
elif filetype.is_dir(location, follow_symlinks=follow_symlinks):
334334
dirs = []
335335
files = []
336-
for entry in os.scandir(location):
337-
loc = os.path.join(location, entry.name)
336+
for resource in os.scandir(location):
337+
loc = os.path.join(location, resource.name)
338338
if filetype.is_special(loc) or (ignored and ignored(loc)):
339-
if follow_symlinks and entry.is_symlink() and not filetype.is_broken_link(location):
339+
if (
340+
follow_symlinks
341+
and resource.is_symlink()
342+
and not filetype.is_broken_link(location)
343+
):
340344
pass
341345
else:
342346
if TRACE:
343347
ign = ignored and ignored(loc)
344348
logger_debug("walk: ignored:", loc, ign)
345349
continue
346350
# special files and symlinks are always ignored
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)
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)
351355
yield location, dirs, files
352356

353357
for dr in dirs:
@@ -396,7 +400,7 @@ def copytree(src, dst):
396400
if not filetype.is_readable(src):
397401
chmod(src, R, recurse=False)
398402

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

401405
if not os.path.exists(dst):
402406
os.makedirs(dst)
@@ -537,7 +541,7 @@ def _rm_handler(function, path, excinfo): # NOQA
537541
"""
538542
if TRACE:
539543
logger_debug("_rm_handler:", "path:", path, "excinfo:", excinfo)
540-
if function in (os.rmdir, os.listdir):
544+
if function in (os.rmdir, os.listdir, os.scandir):
541545
try:
542546
chmod(path, RW, recurse=True)
543547
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)