From 82a6404b40dc215c66ca1f6f75a61d4a918ce6ac Mon Sep 17 00:00:00 2001 From: Castro0o Date: Tue, 18 Feb 2020 21:47:27 +0100 Subject: [PATCH] padding folder files with zeros for correct order --- functions.py | 22 +++++++++++++--------- upload_imgs_dir.py | 1 - 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/functions.py b/functions.py index 09b7305..b7b9035 100644 --- a/functions.py +++ b/functions.py @@ -1,4 +1,4 @@ -import os, json, re, shlex +import os, json, re, shlex, sys import subprocess from datetime import datetime @@ -124,29 +124,33 @@ def listimgs(dir): def reorder_imgs(dir, dry): + # does zero pad file numbers + lsimgs = listimgs(dir) for img in lsimgs: img_name, img_ext = os.path.splitext(img) - + print(img) # does file follow \d{1,}\.img_ext numb_exp = re.compile( r'(?P.*?)(?P\d+)(?P%s)'% re.escape(img_ext)) match = re.search(numb_exp, img) if not match: print(f'Image {img} Filename is not suitable for bulk upload.' - f'Filename pattern does not match 1.jpg 01.jpg' + f'Filename pattern dn\'t match 1.jpg 01.jpg something01.jpg' f'You have to DO IT MANUALLY') + sys.exit() else: # only change name of single digit numbers if len(match.groupdict()['num']) == 1: name = match.groupdict()['name'] - num = match.groupdict()['num'].zfill(2) # pad with 0 + num = match.groupdict()['num'].zfill(3) # pad with 0s ext = match.groupdict()['ext'] new_img = name + num + ext + src_img = os.path.join(dir, img) + dst_img = os.path.join(dir, new_img) + print(f'Renaming: {img} >>>>> {new_img}') if dry == False: - os.rename(os.path.join(dir, img), - os.path.join(dir, new_img)) - print(f'Renaming: {img} >>>>> {new_img}') - - return listimgs(dir) + os.replace(src_img, dst_img) + lsimgs = listimgs(dir) # update list w/ renamed imgs + return listimgs(dir) diff --git a/upload_imgs_dir.py b/upload_imgs_dir.py index 58e2154..062e896 100644 --- a/upload_imgs_dir.py +++ b/upload_imgs_dir.py @@ -44,7 +44,6 @@ elif len(list(site.ask(f'[[Title::{args.title}]]'))) > 0: sys.exit() -# TODO os.listdir(args.dir) ORDER! lsimgs = reorder_imgs(dir=args.dir, dry=args.dry)