diff roundup/backends/blobfiles.py @ 8088:1045425c23b2

refactor!: replace os.listdir() with os.scandir() In many places we did a listdir() then a stat to see if it's a file or directory. This change removes the need for the stat call. Also for larger directories, scandir() is an iterator, so less memory use. There is one remnant of listdir used in an error handler. That requires a stat on each element in the directory, so there is no benefit to using scandir() other than a slight memory saving on a rarely used piece of code. BREAKING CHANGE: Python 2 requires installation of scandir pip package after this commit.
author John Rouillard <rouilj@ieee.org>
date Tue, 16 Jul 2024 01:05:49 -0400
parents 16d6d1f9038a
children 586f76eb33e8
line wrap: on
line diff
--- a/roundup/backends/blobfiles.py	Sun Jul 14 13:36:06 2024 -0400
+++ b/roundup/backends/blobfiles.py	Tue Jul 16 01:05:49 2024 -0400
@@ -27,12 +27,11 @@
     if not os.path.exists(dir):
         return 0
     num_files = 0
-    for dir_entry in os.listdir(dir):
-        full_filename = os.path.join(dir, dir_entry)
-        if os.path.isfile(full_filename):
+    for dir_entry in os.scandir(dir):
+        if dir_entry.is_file():
             num_files = num_files + 1
-        elif os.path.isdir(full_filename):
-            num_files = num_files + files_in_dir(full_filename)
+        elif dir_entry.is_dir():
+            num_files = num_files + files_in_dir(dir_entry.path)
     return num_files
 
 

Roundup Issue Tracker: http://roundup-tracker.org/