Skip to content

Instantly share code, notes, and snippets.

@Adam-S-Amir
Created October 15, 2023 23:52
Show Gist options
  • Save Adam-S-Amir/e5417c89ad1098e9ab4df2aaf58370b9 to your computer and use it in GitHub Desktop.
Save Adam-S-Amir/e5417c89ad1098e9ab4df2aaf58370b9 to your computer and use it in GitHub Desktop.
Folder File Tree to JSON
# 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