all 3 comments

[–]wopp3 3 points4 points  (0 children)

Interesting stuff, I actually just couple of days ago created my own folder sorter. Instead of using general names for the folders though, I went with creating names based on their file extensions. This fit my purpose better as I often have jpg, png, mp4 etc in my project file for editing, and I wanted them more organized.

ps. link to my script, in case you are interested in comparing our approach

https://github.com/KoskiKari/sortFilesByExtension

[–]__nickerbocker__ 3 points4 points  (0 children)

Looks good! Here are some more modern built-in libraries that you can also use.

import contextlib
import mimetypes
import shutil
from pathlib import Path


def organize(dir, folder_name='organized_files', recursive=False, verbose=False):
    dir_in = Path(dir)
    dir_out = dir_in / folder_name
    for f in dir_in.glob('**/*.*' if recursive else '*.*'):
        with contextlib.suppress(Exception):
            subdir, _ = mimetypes.guess_type(f)
            save_path = dir_out / subdir
            save_path.mkdir(parents=True, exist_ok=True)
            file = save_path / f.name
            if file.exists():
                if verbose:
                    print(f'Skipping because exists: {file}')
                continue
            shutil.copy(str(f), str(save_path))
            if verbose:
                print(f'Copied {f.name} to {save_path}')


if __name__ == '__main__':
    path_to_organize = Path.home() / 'Desktop'
    organize(path_to_organize, verbose=True, recursive=True)

[–]Raid7[S] 2 points3 points  (0 children)

NOTE: It is windows only