Created
March 3, 2022 17:13
-
-
Save lukasgabriel/2cc1613a8eaa9dfd0f567e5b5eeb5db3 to your computer and use it in GitHub Desktop.
Groups files into folders by the first letter of the filename. If a threshold is set, the script makes sure that the number of items in a directory doesn't exceed the threshold, creating a new "sub-layer" by second letter (and third, fourth, ...) instead, until MAX_DEPTH is reached.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# group_files.py | |
from pathlib import WindowsPath | |
import os | |
THRESHOLD_MODE = True | |
THRESHOLD = 32 | |
MAX_LEVELS = 4 | |
SOURCE_PATH = WindowsPath("C:\\PATH\\TO\\FOLDER\\") | |
def subdivide(src, lv=0): | |
if lv < MAX_LEVELS: | |
for item in src.iterdir(): | |
if item.is_file(): | |
if not WindowsPath(f"{src}\\{item.name[0:lv+1]}\\").exists(): | |
os.mkdir(f"{src}\\{item.name[0:lv+1]}") | |
try: | |
os.replace( | |
item.resolve(), f"{src}\\{item.name[0:lv+1]}\\{item.name}" | |
) | |
except Exception as e: | |
print(e) | |
for folder in src.iterdir(): | |
if folder.is_dir(): | |
if ( | |
sum(1 for item in folder.iterdir()) > THRESHOLD | |
or not THRESHOLD_MODE | |
): | |
subdivide(folder, lv + 1) | |
subdivide(SOURCE_PATH) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Needs some work, though.