Created
July 17, 2017 05:14
-
-
Save dangpzanco/3627425b9731fee84c9cf0a9e60ccda7 to your computer and use it in GitHub Desktop.
Rename files inside a comic book images folder [Python]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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