Dynamically rename subfolders with regex

Viewed 65

I have a folder structure for my comics that goes along the lines of

C:\Comics\Titles\Chapters\*.jpgs

What I am trying to do is rename all of the chapter folders for every comic in C:\Comics to follow the format "Chapter_01", Chapter_02" etc. Currently the chapter folders have the correct numbers but they also have the title in them, so I'd want the regex to replace the filename from something like "Battle_Royale_Issue_01" to just "Chapter_01". This regex replace should work fine as far as I can see.

Find: ([a-zA-Z0-9_]+)(_\d+)

Replace: Chapter$2

So far every post I've found that shows how to replace folder names is either in a specific directory or goes through every directory recursively. Since I just need to loop through the chapter directory levels neither of those options are very useful for me. Here is what I've got so far, where I am able to get the path to every title folder, but I don't know how to loop through that list to generate a list of the chapter folders.

import os
import re

#Sets Directory
path = "C:/Comics/Titles/Chapters/"

titles=[]

#Gets path of all comic titles
for dirs in next(os.walk(path))[1]:
    for dir in dirs:
        #append the folder name to the title list
        titles.append(os.path.join(path, dirs))

for title in titles:
    print(title)

This is what I though would work to get the subdirectories of the title list

chapters=[]

for dirnames in titles:
    for dir in dirnames:
        #append the folder name to the chapter list
        chapters.append(os.path.join(path, dirnames))

for chapter in chapters:
    print(chapters)

But when I run all of it together the terminal printed results for at least a minute before I killed it and the results were just the path to the title folder again so clearly I don't have it right...

For the regex I was thinking I could use this but altered to look for folders instead of files

import os, glob, sys, re    
os.chdir(sys.argv[1])
for filename in glob.glob("IMG_*.jpg"):
    newfilename = re.sub(?????
    try:
       os.rename(filename,newfilename)
    except OSError,e:
       print e

Any help would be very much appreaciated!

1 Answers

I set the file path to one folder higher
For folders, break after the first run

import os
import re

#Sets Directory
path = "C:/Comics/Titles/"

titles=[]

#Gets path of all comic titles
for dirs in next(os.walk(path))[1]:
    for dir in dirs:
        #append the folder name to the title list
        titles.append(os.path.join(path, dirs))
        break

for title in titles:
    titleSplitlist = title.split("/")
    fileNameOnly = titleSplitlist[-1]
    newFilename = re.sub('[a-zA-Z0-9_]+(?=_\d+$)','Chapter',title)
    print(title,'-->',newFilename)
    os.rename(title,newFilename)

When running this, it prints renames the folders and prints out:

C:/Comics/Titles/LargeTest_04 --> C:/Comics/Titles/Chapter_04
C:/Comics/Titles/Large_Chapter_05 --> C:/Comics/Titles/Chapter_05
Related