Skip to content

Instantly share code, notes, and snippets.

@fulcrum6378
Last active July 2, 2024 14:06
Show Gist options
  • Save fulcrum6378/2430eee1e36199ae629b2d3f770d0223 to your computer and use it in GitHub Desktop.
Save fulcrum6378/2430eee1e36199ae629b2d3f770d0223 to your computer and use it in GitHub Desktop.
Map all files in a path recursively into a Json file
import json
import os
def folder(path: str):
global root
children = list()
try:
mass = os.listdir(path)
except PermissionError:
return children
for x in mass:
item = os.path.join(path, x)
if os.path.isdir(item):
try:
children.append(folder(item))
except RecursionError:
children.append(x)
elif os.path.isfile(item):
children.append(x)
else:
children.append(x)
return {os.path.basename(path): children} if path != root else children
root = os.getcwd()
open(os.path.join(root, os.path.basename(os.getcwd()) + '.json'), 'w', encoding='utf-8').write(
json.dumps(folder(root), indent=2, sort_keys=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment