Skip to main content

Remove the emptydir.py script

ID
060e71c
date
2024-06-18 21:24:28+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
5bf47d9
message
Remove the `emptydir.py` script

For https://github.com/alexwlchan/scripts/issues/48
changed files
2 files, 1 addition, 84 deletions

Changed files

fs/README.md (4727) → fs/README.md (4177)

diff --git a/fs/README.md b/fs/README.md
index a25e139..2f4d991 100644
--- a/fs/README.md
+++ b/fs/README.md
@@ -36,10 +36,6 @@ scripts = [
         "description": "prints the directory which is the deepest child of the given directory"
     },
     {
-        "name": "emptydir.py",
-        "description": "removes any empty directories under the current one (including directories that are empty aside from files that can be safely deleted, e.g. <code>.DS_Store</code>)"
-    },
-    {
         "name": "flatten",
         "description": """
         flattens a directory structure.
@@ -104,15 +100,6 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
   </dd>
 
   <dt>
-    <a href="https://github.com/alexwlchan/scripts/blob/main/fs/emptydir.py">
-      <code>emptydir.py</code>
-    </a>
-  </dt>
-  <dd>
-    removes any empty directories under the current one (including directories that are empty aside from files that can be safely deleted, e.g. <code>.DS_Store</code>)
-  </dd>
-
-  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/fs/flatten">
       <code>flatten</code>
     </a>
@@ -169,4 +156,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
       7.58M ~/repos/scripts</code></pre></p>
   </dd>
 </dl>
-<!-- [[[end]]] (checksum: 632a7aa9e05edd9243371bf85c14b266) -->
+<!-- [[[end]]] (checksum: 1b70ade732604da8223fe9628b2ebf80) -->

fs/emptydir.py (1665) → fs/emptydir.py (0)

diff --git a/fs/emptydir.py b/fs/emptydir.py
deleted file mode 100755
index b08b885..0000000
--- a/fs/emptydir.py
+++ /dev/null
@@ -1,70 +0,0 @@
-#!/usr/bin/env python3
-"""
-This script walks a directory tree, looks for empty directories,
-and removes them.
-
-It prints the name of every directory it removes.
-"""
-
-import os
-import shutil
-import sys
-
-import humanize
-import termcolor
-
-
-def can_be_deleted(d):
-    # This is a folder where I put files that I explicitly don't
-    # want to put in backups.  See https://overcast.fm/+R7DX9_W-Y/21:22
-    # or my Obsidian note about the same.
-    if os.path.abspath(d) == "/Users/alexwlchan/Desktop/do not back up":
-        return False
-
-    entries = os.listdir(d)
-
-    if not entries:
-        return True
-
-    if all(name in {".DS_Store", "__pycache__", ".venv"} for name in entries):
-        return True
-
-    return False
-
-
-def delete_directory(d):
-    assert can_be_deleted(d)
-    print(d)
-    shutil.rmtree(d)
-
-    # Was this the only entry in its parent directory?  Unwind one level
-    # to see if we can delete the parent also.
-    parent = os.path.dirname(d)
-    if can_be_deleted(parent):
-        return 1 + delete_directory(parent)
-    else:
-        return 1
-
-
-if __name__ == "__main__":
-    total_deleted = 0
-
-    try:
-        root = sys.argv[1]
-    except IndexError:
-        root = "."
-
-    for d, _, _ in os.walk(root):
-        if can_be_deleted(d):
-            total_deleted += delete_directory(d)
-
-    if total_deleted == 1:
-        print(termcolor.colored("1 directory deleted", "green"))
-    elif total_deleted > 0:
-        print(
-            termcolor.colored(
-                f"{humanize.intcomma(total_deleted)} directories deleted", "green"
-            )
-        )
-    else:
-        print(termcolor.colored("No empty directories found", "blue"))