Skip to content

Instantly share code, notes, and snippets.

@cryocaustik
Last active February 20, 2020 21:08
Show Gist options
  • Save cryocaustik/42d4ac6aff055429c9ce611ec15874d4 to your computer and use it in GitHub Desktop.
Save cryocaustik/42d4ac6aff055429c9ce611ec15874d4 to your computer and use it in GitHub Desktop.
Python script to walk a target directory and output a file of all child paths, grouped by length.
from pathlib import Path
import json
def path_analysis(target_dir, export_path, unix_path=False):
target_dir = Path(target_dir)
export_path = Path(export_path)
files = list({_ for _ in target_dir.rglob("*")})
files.sort(reverse=True, key=lambda x: len(str(x)))
path_dict = dict()
for f in files:
_len = len(str(f))
_fn = str(f)
if unix_path:
_fn = _fn.replace("\\", "/")
if _len not in path_dict:
path_dict[_len] = []
path_dict[_len].append(_fn)
with open(export_path, "w") as _f:
json.dump(path_dict, _f, indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment