From 00735da543d1b79354cd564d93361cc6b8e83e6e Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 19 May 2024 16:05:19 -0400 Subject: [PATCH] Fixed formatting when counting files in subdirectories - now does not show 0 if the directory contains no non-folder files. More logic conditions needed to ensure correct formatting when omitting this --- CountFolderFiles.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/CountFolderFiles.py b/CountFolderFiles.py index c14b3e5..6588105 100644 --- a/CountFolderFiles.py +++ b/CountFolderFiles.py @@ -42,9 +42,14 @@ def count_folder_files(folder_path): else: file_count = "" subdirectories = get_subdirectories(folder_path) - for directory in subdirectories: - file_count = file_count + str(sum(1 for entry in os.scandir(directory) if entry.is_file())) + " " - file_count = file_count + str(sum(1 for entry in os.scandir(folder_path) if entry.is_file())) + for i, directory in enumerate(subdirectories): + file_count = file_count + str(sum(1 for entry in os.scandir(directory) if entry.is_file())) + #If there's another element, add a space (Keep code this way so that append folder files looks right) + if i != len(subdirectories) - 1: + file_count = file_count + " " + #Append the number of files in the folder unless if there are none + if (sum(1 for entry in os.scandir(folder_path) if entry.is_file()) > 0): + file_count = file_count + " " + str(sum(1 for entry in os.scandir(folder_path) if entry.is_file())) print(subdirectories) print(file_count) return file_count