/
sejeenn
/
python_basic
Обзор
Документация
Войти
/
sejeenn
/
python_basic
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Module26/05_str_count/main.py
33 строки
2 KB
Eugene Vorontsov
Copy repo python basic
03 фев 2025, 20:23
03 фев 2025, 20:23
a8fce4a
Код
Авторство
О чём код?
import os from collections.abc import Iterable def find_directory(path: str) -> Iterable[str]: """Поиск в директории и ее поддиректориях всех файлов""" for i_elem in os.listdir(path): current_path = os.path.join(path, i_elem) if os.path.isdir(current_path): yield from find_directory(current_path) elif os.path.isfile(current_path): yield current_path line_count = 0 directory_python_files = 'C:\\Users\\Professional\\PycharmProjects\\Python_Basic\\Module25' result = find_directory(path=directory_python_files) filtered_files = [abs_path for abs_path in result if abs_path.endswith('.py')] print('Все файлы с расширением *.py в директории {}'.format(directory_python_files)) for file_path in filtered_files: print('В файле', file_path, end=' ') with open(file_path, 'r', encoding="UTF-8") as file: local_count = 0 for line in file.read().split('\n'): if not line.strip() == '' and not line.strip().startswith('#') \ and not line.strip().startswith('"'): local_count += 1 print('____{}____строк'.format(local_count)) line_count += local_count print('Всего в директории {} и её поддиректориях,в файлах с расширением ' '*.py {} строк кода.'.format(directory_python_files, line_count)) print('Пустые строки и строчки комментариев проигнорированы.')