Blog Logo

Oct 25 2024 ~ 4 min read

Automated Folder Structure Generator


Next conf banner.

Inspired by my last blog post on creating a Python script for organizing files, I decided to create a script that would generate an organized representation of project structure in markdown format. This new tool would not only help me visualize my folders and files but also serve as a handy resource for creating GitHub README files. With a simple run, I could generate a neat overview of my projectsβ€”making documentation easy.

The Idea Takes Shape

I thought of a beautifully formatted markdown output that listed my folders and files, complete with icons to make it visually appealing. I chose the classic folder emoji (πŸ—‚οΈ) for directories and a document emoji (πŸ“„) for files.

Here’s a the main function of the code:


def generate_markdown_structure(root_dir, skip_folders, indent=''):
    markdown_structure = ''
    items = sorted(os.listdir(root_dir))

    for index, item in enumerate(items):
        if item in skip_folders:
            continue

        path = os.path.join(root_dir, item)
        if os.path.isdir(path):
            icon = 'πŸ—‚οΈ'
            connector = '└── ' if index == len(items) - 1 else 'β”œβ”€β”€ '
            markdown_structure += f"{indent}{connector}{icon} {item}\n"
            sub_indent = indent + ('    ' if connector == '└── ' else 'β”‚   ')
            markdown_structure += generate_markdown_structure(path, skip_folders, sub_indent)
        else:
            icon = 'πŸ“„'
            connector = '└── ' if index == len(items) - 1 else 'β”œβ”€β”€ '
            markdown_structure += f"{indent}{connector}{icon} {item}\n"

    return markdown_structure

This piece of code recursively explores the directory and generates a structured output while skipping unwanted folders.

Sample Output

β”œβ”€β”€ πŸ“„ .gitignore
β”œβ”€β”€ πŸ“„ CHANGELOG.md
β”œβ”€β”€ πŸ“„ README.md
β”œβ”€β”€ πŸ“„ eslint.config.js
β”œβ”€β”€ πŸ“„ index.html
β”œβ”€β”€ πŸ“„ package-lock.json
β”œβ”€β”€ πŸ“„ package.json
β”œβ”€β”€ πŸ“„ postcss.config.js
β”œβ”€β”€ πŸ—‚οΈ public
β”‚   └── πŸ“„ vite.svg
β”œβ”€β”€ πŸ—‚οΈ src
β”‚   β”œβ”€β”€ πŸ“„ App.css
β”‚   β”œβ”€β”€ πŸ“„ App.tsx
β”‚   β”œβ”€β”€ πŸ—‚οΈ assets
β”‚   β”‚   └── πŸ“„ react.svg
β”‚   β”œβ”€β”€ πŸ—‚οΈ component
β”‚   β”‚   β”œβ”€β”€ πŸ—‚οΈ add-expense
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ AddExpense.module.css
β”‚   β”‚   β”‚   └── πŸ“„ AddExpense.tsx
β”‚   β”‚   β”œβ”€β”€ πŸ—‚οΈ expense-grid
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ ExpenseGrid.module.css
β”‚   β”‚   β”‚   └── πŸ“„ ExpenseGrid.tsx
β”‚   β”‚   β”œβ”€β”€ πŸ—‚οΈ home
β”‚   β”‚   β”‚   └── πŸ“„ Home.tsx
β”‚   β”‚   β”œβ”€β”€ πŸ—‚οΈ navbar
β”‚   β”‚   β”‚   β”œβ”€β”€ πŸ“„ Navbar.module.css
β”‚   β”‚   β”‚   └── πŸ“„ Navbar.tsx
β”‚   β”‚   └── πŸ—‚οΈ sidebar
β”‚   β”‚       β”œβ”€β”€ πŸ“„ Sidebar.module.css
β”‚   β”‚       └── πŸ“„ Sidebar.tsx
β”‚   β”œβ”€β”€ πŸ“„ index.css
β”‚   β”œβ”€β”€ πŸ“„ main.tsx
β”‚   β”œβ”€β”€ πŸ“„ types.ts
β”‚   └── πŸ“„ vite-env.d.ts
β”œβ”€β”€ πŸ“„ tailwind.config.js
β”œβ”€β”€ πŸ“„ tsconfig.app.json
β”œβ”€β”€ πŸ“„ tsconfig.json
β”œβ”€β”€ πŸ“„ tsconfig.node.json
└── πŸ“„ vite.config.ts

Next Steps

Now that we have a powerful script to organize the folder structure in markdown format, it’s time to think about how to make the most of it. One exciting possibility is to integrate this script into your release process or CI/CD pipeline. By doing so, you can ensure that your README file is always up to date with the latest project structure without any manual effort.

Automating with CI/CD

Here’s how you can set up your CI/CD pipeline to automatically generate or update your README file:

  1. Add the Script to Your Repository: Make sure the script is part of your repository. Place it in a logical location, such as a /scripts folder.

  2. Create a Workflow: If you’re using GitHub Actions, you can create a workflow that triggers on specific events, like when you push new commits or create a release. Your workflow file (e.g., .github/workflows/update-readme.yml) might look something like this:

name: Update README

on:
  push:
    branches:
      - main
  release:
    types: [created]

jobs:
  update-readme:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.8'  # Python version

      - name: Install dependencies
        run: |
          pip install -r requirements.txt  # If any dependencies

      - name: Run folder structure script
        run: python scripts/your_script.py  # Update with script path

      - name: Commit and push changes
        run: |
          git config --local user.email "[email protected]"
          git config --local user.name "GitHub Action"
          git add project_structure.md
          git commit -m "Update project structure in README" || echo "No changes to commit"
          git push

  • Update Your README: In your script, you can modify it to not only generate a project_structure.md file but also append or integrate its contents directly into your main README.md. This way, you’ll always have an up-to-date overview of your project structure right on your project’s main page.

  • Test the Workflow: Push your changes to the repository and check the Actions tab on GitHub to see if the workflow runs successfully.

You can access the full code on my GitHub.


Headshot of Samarth

Hi, I'm Samarth. I'm a software engineer based in Los Angeles. You can follow me on Twitter, see some of my work on GitHub, or read more about me on LinkedIn.