Skip to content

Instantly share code, notes, and snippets.

@dangpzanco
Created July 17, 2017 05:14
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 dangpzanco/3627425b9731fee84c9cf0a9e60ccda7 to your computer and use it in GitHub Desktop.
Save dangpzanco/3627425b9731fee84c9cf0a9e60ccda7 to your computer and use it in GitHub Desktop.
Rename files inside a comic book images folder [Python]
from pathlib import Path
from glob import glob
import os
import math
def batchRename(folderPath='.', preffix='p'):
if isinstance(folderPath, str):
folderPath = Path(folderPath)
folderName = folderPath.absolute().name
fileList = os.listdir(folderPath)
numDigits = math.ceil(math.log10(len(fileList)))
for n, file in enumerate(fileList):
number = str(n+1)
number = (numDigits - len(number)) * '0' + number
newFilename = folderName + ' ' + preffix + number + '.' + file.split('.')[-1]
os.renames(folderPath / file, folderPath / newFilename)
print(folderPath / file, '-->', newFilename)
folderList = glob('./*/')
folderPath = [Path(f) for f in folderList]
for folder in folderPath:
batchRename(folder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment