1. import os
  2. import json
  3. def encode_directories(root_dir, output_file="directories.json"):
  4. """
  5. Encodes directory structure for validation checks with synchronous execution.
  6. Outputs a JSON file containing the directory tree.
  7. """
  8. directory_data = {}
  9. for root, dirs, files in os.walk(root_dir):
  10. # Remove the root directory from the path
  11. relative_path = os.path.relpath(root, root_dir)
  12. directory_data[relative_path] = []
  13. for dir_name in dirs:
  14. dir_path = os.path.join(root, dir_name)
  15. relative_dir_path = os.path.relpath(dir_path, root_dir)
  16. directory_data[relative_path].append(relative_dir_path)
  17. for file_name in files:
  18. file_path = os.path.join(root, file_name)
  19. relative_file_path = os.path.relpath(file_path, root_dir)
  20. directory_data[relative_path].append(relative_file_path)
  21. with open(output_file, "w") as f:
  22. json.dump(directory_data, f, indent=4) # Write to JSON file with indentation
  23. print(f"Directory structure encoded to {output_file}")
  24. if __name__ == '__main__':
  25. # Example usage:
  26. root_directory = "." # Current directory
  27. encode_directories(root_directory)

Add your comment