Skip to content

Instantly share code, notes, and snippets.

@veirus
Created June 7, 2018 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veirus/2d72c6203854c44ed2d83b830dc21add to your computer and use it in GitHub Desktop.
Save veirus/2d72c6203854c44ed2d83b830dc21add to your computer and use it in GitHub Desktop.
My *favourite* sorting script now with **Pathlib**
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
# dror - pathlib edition
# Based on https://gist.github.com/dansku/9040240#file-dropboxorganize-py
__lastchanged__ = '2018-06-07 06:07'
__verstion__ = '0.6.2'
from datetime import date
from pathlib import Path
def move(files, newname):
try:
files.rename(newname)
except FileExistsError:
print(f'[!] File {newname} already exists, removing duplicate.')
files.unlink()
else:
print(f'Movendo: {files} --> {newname}')
def main(method=0):
fileFormats = ('ai', 'bmp', 'cdr', 'jpg', 'jpeg', 'mov', 'png', 'pdf',
'psd', 'mp4', 'gif', 'webm', 'webp', 'svg', 'flv')
twi = ('jpg_large', 'jpg-large', 'jpg-orig', 'png-large', 'png-orig',
'jpg_orig', 'png_orig')
fileFormats = fileFormats + twi
root = Path().cwd()
print(f'***42*** {root}:')
filelist = (i for f in fileFormats for i in root.glob(f'*.{f}'))
for files in filelist:
fixed = ''
# Retrieving creation date (getctime() is Windows only):
# modification time is more prefered actually:
ctime = date.fromtimestamp(files.stat().st_mtime)
newsub = root / f'{ctime:%Y-%m-%d}'
if method:
newsub = root / f'{ctime:%Y}' / f'{ctime:%m}'
if files.suffix.endswith(twi):
fixed = files.with_suffix(files.suffix[:4])
fname = fixed or files
newname = newsub / fname.name
if not newsub.exists():
print('Making dir:', newsub)
newsub.mkdir()
move(files, newname)
print(f"Done :) with method {method}")
if __name__ == '__main__':
main(0)
@veirus
Copy link
Author

veirus commented Jun 7, 2018

Apparently i really like to move files, i already made like four different versions of this shit here...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment