count_folder_files will now return a different result if more than 0 subfolders
This commit is contained in:
parent
e0410dbe3e
commit
a80d99f871
|
|
@ -15,12 +15,27 @@ def append_to_folder_name(folder_path, suffix):
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print(f"Error: Folder '{folder_path}' not found.")
|
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):
|
def count_folder_files(folder_path):
|
||||||
try:
|
try:
|
||||||
|
# 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
|
# Count the number of files in the folder
|
||||||
file_count = sum(1 for entry in os.scandir(folder_path) if entry.is_file())
|
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}")
|
print(f"Number of files in the folder: {file_count}")
|
||||||
return 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:
|
except FileNotFoundError:
|
||||||
print(f"Error: Folder '{folder_path}' not found.")
|
print(f"Error: Folder '{folder_path}' not found.")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user