count_folder_files will now return a different result if more than 0 subfolders

This commit is contained in:
admin 2024-05-19 15:13:16 -04:00
parent e0410dbe3e
commit a80d99f871

View File

@ -15,12 +15,27 @@ def append_to_folder_name(folder_path, suffix):
except FileNotFoundError:
print(f"Error: Folder '{folder_path}' not found.")
def count_subdirectories(directory):
subdirectory_count = 0
for _, dirnames, _ in os.walk(directory):
subdirectory_count += len(dirnames)
return subdirectory_count
def count_folder_files(folder_path):
try:
# Count the number of files in the folder
file_count = sum(1 for entry in os.scandir(folder_path) if entry.is_file())
print(f"Number of files in the folder: {file_count}")
return file_count
# If no subfolders exist, return the number of files in the folder
subdirectories = count_subdirectories(folder_path)
if subdirectories == 0:
# Count the number of files in the folder
file_count = sum(1 for entry in os.scandir(folder_path) if entry.is_file())
print(f"Number of files in the folder: {file_count}")
return file_count
# If subdirectories do exist, return the number of files in each subdirectory then this directory
else:
print(">0")
except FileNotFoundError:
print(f"Error: Folder '{folder_path}' not found.")