Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.