Created
October 15, 2023 23:52
-
-
Save Adam-S-Amir/e5417c89ad1098e9ab4df2aaf58370b9 to your computer and use it in GitHub Desktop.
Folder File Tree to JSON
This file contains hidden or 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
# This file is made to create a JSON with the current folder's | |
# file's in a file tree that look like this: | |
# "Images": { | |
# "Apple.png": null, | |
# "Banana.png": null, | |
# "Orange.png": null, | |
# "Grape.png": null, | |
# "Blueberry.png": null | |
# } | |
import os | |
import json | |
def generate_dir_structure(path): | |
result = {} | |
for item in os.listdir(path): | |
item_path = os.path.join(path, item) | |
if os.path.isdir(item_path): | |
result[item] = generate_dir_structure(item_path) | |
else: | |
result[item] = None | |
return result | |
current_directory = os.getcwd() | |
dir_structure = generate_dir_structure(current_directory) | |
json_structure = json.dumps(dir_structure, indent=2) | |
print(json_structure) | |
f = open("file-tree.json", "w") | |
f.write(json_structure) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment