padding folder files with zeros for correct order

docker
Castro0o 5 years ago
parent 59ba70bebb
commit 82a6404b40

@ -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<name>.*?)(?P<num>\d+)(?P<ext>%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)

@ -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)

Loading…
Cancel
Save