user2705263
user2705263

Reputation: 13

Renaming File Names Using Python

Sorry if this question is too basic, I've been searching python and regex and haven't found exactly what I need.

I have a bunch of television show files with names that end in S01EXX like the following:

filename.S01E01.mkv
filename.S01E02.mkv
filename.S01E03.mkv

Each of this is actually a double episode, so the above three should be named with the form:

filename.S01E01-E02.mkv
filename.S01E03-E04.mkv
filename.S01E05-E06.mkv

so each should be the episode number times 2 minus 1, and the episode number times 2. I know how to rename and split with python, I just don't know how to go about parsing out the episode numbers and how deal with the leading zero that will disappear when we get to 2 digit episode numbers.

Any help is much appreciated!

Upvotes: 1

Views: 712

Answers (1)

Brent Washburne
Brent Washburne

Reputation: 13138

If the position of the episode number is constant, you can extract it using a slice:

e = int(filename[-6:-4])
new_filename = '%s%02d-E%02d%s' % (filename[:-6], e*2-1, e*2, filename[-4:])

Upvotes: 1

Related Questions