Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Last active January 7, 2019 20:33
Show Gist options
  • Save yitsushi/b0fce457b1752a430396e9c6a9faccf8 to your computer and use it in GitHub Desktop.
Save yitsushi/b0fce457b1752a430396e9c6a9faccf8 to your computer and use it in GitHub Desktop.
Archive files (photos/screenshots/videos etc). I have when I have 1000+ files in a single directory
#!/usr/bin/env python3
import os
from datetime import datetime
from multiprocessing import Pool
def is_image(x):
return x.endswith('.jpg') or x.endswith('.png')
def is_movie(x):
return x.endswith('.mp4') or x.endswith('.webm')
files = [f for f in os.listdir('.') if is_image(f) or is_movie(f)]
def move_file(filename):
mtime = int(os.stat(filename).st_mtime)
dirname = datetime.utcfromtimestamp(mtime).strftime('%Y-%m')
if is_movie(filename):
dirname = f'{dirname}-video'
if not os.path.isdir(dirname):
try:
# Not thread-safe, but I don't care
os.mkdir(dirname)
except FileExistsError:
pass
os.rename(filename, os.path.join(dirname, filename))
with Pool(8) as p:
p.map(move_file, files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment