Spaces:
Sleeping
Sleeping
import os | |
import json | |
# Define a set of multimedia file extensions | |
MULTIMEDIA_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', | |
'.mp3', '.wav', '.aac', '.flac', | |
'.mp4', '.avi', '.mov', '.mkv', '.wmv'} | |
def get_directory_structure(root_dir): | |
def build_structure(current_dir): | |
structure = [] | |
for item in os.listdir(current_dir): | |
item_path = os.path.join(current_dir, item) | |
if os.path.isdir(item_path): | |
structure.append({ | |
"name": item, | |
"type": "directory", | |
"content": None, | |
"children": build_structure(item_path) | |
}) | |
else: | |
file_extension = os.path.splitext(item)[1].lower() | |
if file_extension in MULTIMEDIA_EXTENSIONS: | |
# For multimedia files, don't capture the content | |
structure.append({ | |
"name": item, | |
"type": "file", | |
"content": None, # No content for multimedia files | |
"children": [] | |
}) | |
else: | |
with open(item_path, 'r', encoding='utf-8', errors='ignore') as file: | |
file_content = file.read() | |
structure.append({ | |
"name": item, | |
"type": "file", | |
"content": file_content, | |
"children": [] | |
}) | |
return structure | |
return build_structure(root_dir) | |
def generate_json(output_file): | |
current_directory = os.getcwd() | |
structure = get_directory_structure(current_directory) | |
with open(output_file, 'w', encoding='utf-8') as json_file: | |
json.dump(structure, json_file, indent=4) | |
if __name__ == "__main__": | |
output_json_file = "output.json" | |
generate_json(output_json_file) | |
print(f"Directory structure saved to {output_json_file}") | |