Skip to main content

Add a snippet to get directories

ID
816195a
date
2024-04-14 09:05:43+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
b8bec42
message
Add a snippet to get directories
changed files
2 files, 23 additions

Changed files

create_snippets.py (7058) → create_snippets.py (7099)

diff --git a/create_snippets.py b/create_snippets.py
index 61126f4..7595e46 100755
--- a/create_snippets.py
+++ b/create_snippets.py
@@ -221,6 +221,7 @@ SNIPPETS = {
     "py!h": read("create_hash.py"),
     "py!jd": read("datetime_encoder.py"),
     "py!pth": read("get_file_paths.py"),
+    "py!pd": read("get_directories.py"),
     "py!sec": read("get_secrets_manager_secret.py"),
     "py!s3": read("list_s3_objects.py"),
 

expansions/get_directories.py (0) → expansions/get_directories.py (498)

diff --git a/expansions/get_directories.py b/expansions/get_directories.py
new file mode 100644
index 0000000..1ff774d
--- /dev/null
+++ b/expansions/get_directories.py
@@ -0,0 +1,22 @@
+import pathlib
+
+
+def get_directories_under(root="."):
+    """
+    Generates the absolute paths to every directory under ``root``.
+    """
+    root = pathlib.Path(root)
+
+    if root.exists() and not root.is_dir():
+        raise ValueError(f"Cannot find files under file: {root!r}")
+
+    if not root.is_dir():
+        raise FileNotFoundError(root)
+
+    for dirpath, dirnames, _ in root.walk():
+        for d in dirnames:
+            yield dirpath / f
+
+
+for p in get_directories_under():
+    {cursor}