Skip to content

Instantly share code, notes, and snippets.

@HansUXdev
Created November 28, 2023 21:15
Show Gist options
  • Save HansUXdev/d550e9059a84170a10153cd0cf74187c to your computer and use it in GitHub Desktop.
Save HansUXdev/d550e9059a84170a10153cd0cf74187c to your computer and use it in GitHub Desktop.
The script reads the YAML file and parses the directory structure. It then recursively creates directories and files as specified in the YAML file. Before Running the Script: Ensure Python is installed on your system. Install the pyyaml package if not already installed (use pip install pyyaml). Update the yaml_file variable with the correct path…
import os
import yaml
def create_directory_structure(root, structure):
if isinstance(structure, list):
for item in structure:
if isinstance(item, dict):
for key, val in item.items():
dir_path = os.path.join(root, key)
os.makedirs(dir_path, exist_ok=True)
create_directory_structure(dir_path, val)
elif isinstance(item, str):
if '.' in item:
# Create a file
open(os.path.join(root, item), 'w').close()
else:
# Create a directory
os.makedirs(os.path.join(root, item), exist_ok=True)
elif isinstance(structure, dict):
for key, val in structure.items():
dir_path = os.path.join(root, key)
os.makedirs(dir_path, exist_ok=True)
create_directory_structure(dir_path, val)
def main():
yaml_file = 'structure.yaml' # Update with the path to your YAML file
with open(yaml_file, 'r') as file:
directory_structure = yaml.safe_load(file)
for root, structure in directory_structure.items():
os.makedirs(root, exist_ok=True)
create_directory_structure(root, structure)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment