Last active
July 2, 2024 14:06
-
-
Save fulcrum6378/2430eee1e36199ae629b2d3f770d0223 to your computer and use it in GitHub Desktop.
Map all files in a path recursively into a Json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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