Skip to content

Commit 7a3283d

Browse files
authored
Merge pull request #2270 from codalab/compute_worker_function_fix
Compute Worker - fixed function get_folder_size_in_gb
2 parents 3249c53 + 35c8189 commit 7a3283d

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

compute_worker/compute_worker.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,22 @@ def md5(filename):
329329

330330

331331
def get_folder_size_in_gb(folder):
332+
# Check if the folder exists; if not, return 0 GB
332333
if not os.path.exists(folder):
333334
return 0
334-
total_size = os.path.getsize(folder)
335-
for item in os.listdir(folder):
336-
path = os.path.join(folder, item)
337-
if os.path.isfile(path):
338-
total_size += os.path.getsize(path)
339-
elif os.path.isdir(path):
340-
total_size += get_folder_size_in_gb(path)
341-
return total_size / 1000 / 1000 / 1000 # GB: decimal system (1000^3)
335+
336+
total_size = 0 # Initialize total size accumulator (in bytes)
337+
338+
# Walk through the folder and all its subdirectories
339+
for root, dirs, files in os.walk(folder):
340+
for f in files:
341+
# Construct full path to the file
342+
fp = os.path.join(root, f)
343+
# Add the file size to total_size
344+
total_size += os.path.getsize(fp)
345+
346+
# Convert bytes to gigabytes using decimal system (1 GB = 1000^3 bytes)
347+
return total_size / (1000 ** 3)
342348

343349

344350
def delete_files_in_folder(folder):

0 commit comments

Comments
 (0)