Created
March 8, 2023 19:20
-
-
Save cuddlyogre/5b6f110bc35b7c5903e411fa24c970d1 to your computer and use it in GitHub Desktop.
do_move.py
This file contains 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
import glob | |
import os | |
import uuid | |
import random | |
from pathlib import Path | |
import shutil | |
dirs = dict() | |
def do_move(src, dst, max_files=10): | |
paths = glob.glob(os.path.join(src, '*'), recursive=True) | |
random.shuffle(paths) | |
current = 0 | |
dirname: str | |
for path in paths: | |
if not os.path.isfile(path): continue | |
if current % max_files == 0: | |
dirname = os.path.join(dst, str(uuid.uuid4())) | |
Path(dirname).mkdir(parents=True, exist_ok=True) | |
dirs[dirname] = [] | |
i = 0 | |
newname = os.path.basename(path) | |
while os.path.exists(os.path.join(dst, newname)): | |
stem = Path(newname).stem | |
ext = "".join(Path(newname).suffixes) | |
newname = fr"{stem}_{i}{ext}" | |
dest_path = os.path.join(dst, dirname, newname) | |
shutil.copy2(path, dest_path) | |
dirs[dirname].append(path) | |
current += 1 | |
print(dirs) | |
if __name__ == '__main__': | |
src = fr"SOURCE_DIR" | |
dst = fr"DESTINATION_DIR" | |
max_files = 10 | |
do_move(src, dst, 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment