gpt-3.5-turbo-0125· OpenAI
WildChat-4.8M (AI2)
make a function which takes a path as argument,generate a json/dict structure of it’s…
- User
make a function which takes a path as argument,generate a json/dict structure of it’s tree view of format{foldername:contents}
- Assistant
Here is a Python function that takes a path as an argument and generates a JSON/dict structure of its tree view in the format {foldername: contents}:
import os def generate_tree_structure(path): if not os.path.exists(path): return {"Error": "Invalid path"} result = {} for root, dirs, files in os.walk(path): if root == path: result[os.path.basename(root)] = {} for directory in dirs: result[os.path.basename(root)][directory] = [] for file in files: result[os.path.basename(root)].append(file) return result # Example usage: path = "C:/Users/Username/Desktop/TestFolder" tree_structure = generate_tree_structure(path) print(tree_structure)You can pass the path of the folder you want to generate the tree view for to the
generate_tree_structure()function to get the desired JSON/dict structure.