Skip to content

Instantly share code, notes, and snippets.

@Adam-S-Amir
Created October 15, 2023 23:52

Revisions

  1. Adam-S-Amir created this gist Oct 15, 2023.
    30 changes: 30 additions & 0 deletions Dir.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    # 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()