1

IDLE (Python 3.10 64-bit) Win 10

Во рабочий скрипт:

import os

path = 'E:\Мой Python\__Видеокурсы Python'
rez = sorted(os.listdir(path))
with open("out_ok.txt", "w") as file:
    for n, item in enumerate(rez):
        file.write(f"{n + 1} {item}\n")
print('Список файлов смотрим здесь - out_ok.txt ')

# поиск перечня файлов в папке и запись списка в out_ok.txt

Как в этот скрипт добавить возможность видеть размер каждой папки и файла в каталоге ?

1

2 ответа 2

1

для файлов

stat = os.stat(item)
file.write(f"{n + 1} {item}\t{stat.st_size}\n")

Для каталогов надо считать рекурсивно сложив размеры файлов.

1

Очевидно, через os модуль

import os
 
size = os.path.getsize(path)
print(size)

где path - путь к файлу или каталогу

Если встроить в скрипт

with open("out_ok.txt", "w") as file:
    for n, item in enumerate(rez):
        file.write(f"{n + 1} {item} {os.path.getsize(os.path.join(path, item))}\n")

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.