from json import dumps from pathlib import Path import os import time SKIPLIST = ('_DIRECTORY.json',) DIRECTORIES_SUPPLIED = ['www/img', 'www/audio', 'www/maps', 'www/data', 'www/Languages'] def legal_file_name(fname: str) -> bool: return not (fname in SKIPLIST or fname.startswith('.')) def create_listing(path: Path, indent: int = 0) -> None: files = [n for n in os.listdir(path) if legal_file_name(n)] with open(path / '_DIRECTORY.json', 'w') as fp: fp.write(dumps(files)) print(' ' * (indent * 2) + f'>> Wrote listing for {path}') for file in files: rel_path = path / Path(file) if rel_path.is_dir(): create_listing(rel_path, indent + 1) for directory in DIRECTORIES_SUPPLIED: print(f'>> Beginning of writing listing of the {directory}') time.sleep(2) create_listing(Path(directory), 1)