/
disciplina
/
stepik-python-debugging
Обзор
Документация
Войти
/
disciplina
/
stepik-python-debugging
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
practice/Module 4/4.1 __debug__/ls.py
72 строки
2 KB
Sergey
Расписан COURSE.MD, добавлены практические примеры
17 мар 2026, 21:33
17 мар 2026, 21:33
9c33d3b
Код
Авторство
О чём код?
import os import sys import argparse from dataclasses import dataclass @dataclass class PathData: name: str size: int ctime: float is_file: bool def __list(path: str) -> list[PathData]: assert path != "", "Path can't be empty" assert os.path.exists(path), "Path doesn't exist" if __debug__: print("Path: ", path, file=sys.stderr) paths = [] try: for p in os.listdir(path): joined_path = os.path.join(path, p) st: os.stat_result = os.stat(joined_path) paths.append( PathData( name=p, size=st.st_size, ctime=st.st_atime, is_file=os.path.isfile(joined_path), ) ) except OSError as e: raise OSError(e) from e if __debug__: print(paths) return paths def __print(paths: list[PathData]) -> None: assert paths != [], "Paths can't be empty" print("=" * 20, end="\n\n") for p in paths: is_file = "file" if p.is_file else "folder" print(f"{p.ctime} | {p.size} | {is_file} | {p.name}") print() print("=" * 20, end="\n\n") if __name__ == "__main__": parser = argparse.ArgumentParser(prog="List of all directories & files") parser.add_argument("--path", required=True) args = parser.parse_args() if __debug__: print("Args: ", args) try: if args.path: paths = __list(args.path) __print(paths) sys.exit(0) except OSError as e: print(e) sys.exit(1)