Skip to content

Instantly share code, notes, and snippets.

@bohdon
Created April 9, 2019 17:40
Show Gist options
  • Select an option

  • Save bohdon/d2e03ae50f1c93eeddd86262a81fcb0a to your computer and use it in GitHub Desktop.

Select an option

Save bohdon/d2e03ae50f1c93eeddd86262a81fcb0a to your computer and use it in GitHub Desktop.
Remove empty dirs and their meta files for a Unity project
#! /usr/bin/env python
import os
import click
@click.command()
@click.argument("path")
@click.option("-n", "--dry-run", is_flag=True, help="dry-run")
def remove_empty_dirs(path, dry_run=False):
"""
Remove any empty directories within the path,
recursively.
"""
if not os.path.isdir(path):
return
for dirpath, dirnames, filenames in os.walk(path, topdown=False, followlinks=False):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
if filepath.endswith('.meta'):
paired_file = filepath.split('.meta')[0]
if not os.path.exists(paired_file):
print("Removing unused meta file: " + filepath)
if not dry_run:
os.remove(filepath)
if not len(filenames) and not len(dirnames):
print("Removing empty folder: " + dirpath)
if not dry_run:
os.rmdir(dirpath)
if __name__ == "__main__":
remove_empty_dirs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment