ДЗ 6

mail@pastecode.io avatar
unknown
python
8 months ago
1.3 kB
4
Indexable
Never
# Газизьянов Рафаиль Рафилевич

import os
from typing import TypedDict


class FileDirCollectorDictResult(TypedDict):
    dirs: list[str]
    files: list[str]


def filedir_collector(folder_path: str) -> FileDirCollectorDictResult:
    normalized_path = os.path.normpath(folder_path)

    result: FileDirCollectorDictResult = {"dirs": [], "files": []}

    is_dir = os.path.isdir(normalized_path)
    if not is_dir:
        return result

    for path, dirnames, filenames in os.walk(normalized_path):
        result["dirs"] += dirnames
        result["files"] += filenames

    return result


def print_filedir_collector_result_to_file(file_path: str, fd_result: FileDirCollectorDictResult) -> None:
    normalized_path = os.path.normpath(file_path)

    with open(normalized_path, "w", encoding="utf-8") as result_file:
        result_file.write(f"\n{'Dirs':-<50}\n")
        for _dir in fd_result["dirs"]:
            result_file.write(f"\t{_dir}\n")

        result_file.write(f"\n{'Files':-<50}\n")
        for file in fd_result["files"]:
            result_file.write(f"\t{file}\n")


path = "D:/downloads"

result = filedir_collector(path)
print_filedir_collector_result_to_file(os.path.join(path, "result.txt"), result)
Leave a Comment