Spaces:
Sleeping
Sleeping
File size: 2,059 Bytes
1ea5c22 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
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}")
|