import os
import json
def encode_directories(root_dir, output_file="directories.json"):
"""
Encodes directory structure for validation checks with synchronous execution.
Outputs a JSON file containing the directory tree.
"""
directory_data = {}
for root, dirs, files in os.walk(root_dir):
# Remove the root directory from the path
relative_path = os.path.relpath(root, root_dir)
directory_data[relative_path] = []
for dir_name in dirs:
dir_path = os.path.join(root, dir_name)
relative_dir_path = os.path.relpath(dir_path, root_dir)
directory_data[relative_path].append(relative_dir_path)
for file_name in files:
file_path = os.path.join(root, file_name)
relative_file_path = os.path.relpath(file_path, root_dir)
directory_data[relative_path].append(relative_file_path)
with open(output_file, "w") as f:
json.dump(directory_data, f, indent=4) # Write to JSON file with indentation
print(f"Directory structure encoded to {output_file}")
if __name__ == '__main__':
# Example usage:
root_directory = "." # Current directory
encode_directories(root_directory)
Add your comment