Я не очень опытный python программист. Но Я хочу сделать свой код максимально быстрым и эффективным, а также написать его чистым . Итак, у меня есть следующий код, который работает хорошо, но не очень быстро, так как у меня есть каталоги больше 4 ТБ, и я выполняю этот код в своей сети. Поэтому я ищу совет, чтобы прочитать все данные по одному пути, вместо того, чтобы выполнять избыточное второе сканирование каталогов и файлов. Любой совет будет оценен!
def get_size_and_fcount_pathlib(scan_path):
"""Gets the total size of given dir and counts how many folders and files are in the given
path directory and return a file count, folder count and all types as a sum"""
root_directory = Path(scan_path)
total_size = 0
all_types_count = 0
file_count = 0
folder_count = 0
for f in root_directory.glob('**/*'):
if f.is_file():
file_count += 1
total_size += f.stat().st_size
if not str(f.name).startswith("."):
all_types_count += 1
if f.is_dir():
folder_count += 1
size_gb = ", ".join(map(str, (round(total_size/1000/1000/1000, 2), 'GB'))).replace(', ', '')
print('Amount of all types searched: {}'.format(all_types_count))
print('Amount of files searched: {}'.format(file_count))
print('Amount of folders searched: {}'.format(folder_count))
print('Directory size in GB: {}'.format(size_gb))
file_count_collection = [size_gb, all_types_count, file_count, folder_count]
return file_count_collection