andres
andres

Reputation: 321

How to rename a file to a substring within the filename?

I'm trying to rename the files in a directory that have the substring "Episode.26" by truncating the words before and after the substring

e.g. 'The.Vision.of.Escaflowne.Episode.26.Eternal.Love.1080p.Dual.Audio.Bluray [BC6DDF99].mkv'

The value to be found will always be Episode.## (## two digits)

Desired result: Episode.26.mkv

Current result: Episode.26.Eternal.Love.1080p.Dual.Audio.Bluray [BC6DDF99].mkv'

I removed the first n characters using python; but, I don't know how to isolate 'Episode.26' efficiently

import os

key = "Episode."

for filename in os.listdir("."):
  if(key in filename):
    index = filename.index(key)
    os.rename(filename, filename[index:])

Upvotes: 1

Views: 351

Answers (4)

Vilas Magare
Vilas Magare

Reputation: 41

in your code, you can use search instead of match and remove .* at the beginning of re

import re
import os

key = "Episode"
regexp = re.compile('%s\.(\d\d).*\.(.+)' % key)

for filename in os.listdir("."):
  match = regexp.search(filename)

  if match:
    episode, file_ext = match.group(1), match.group(2)
    new_name = key + episode + '.' + file_ext

    os.rename(filename, new_name)

Upvotes: 1

JoshuaCS
JoshuaCS

Reputation: 2624

You could use regular expressions, capture the episode number and file extension and create the new name using such data:

Code

import re
import os

key = "Episode"
regexp = re.compile('.*%s\.(\d\d).*\.(.+)' % key)

for filename in os.listdir("."):
  match = regexp.match(filename)

  if match:
    episode, file_ext = match.group(1), match.group(2)
    new_name = key + episode + '.' + file_ext

    os.rename(filename, new_name)

This way is more cleaner and flexible. REs are very powerfull. Let me know if this worked for you.

Upvotes: 1

Bob Fred
Bob Fred

Reputation: 211

If u'r sure there's two digits after "Episode.", then u can code like this. Otherwise, i'm afraid u should use re or split to get what u want.

import os

key = 'Episode'
for filename in os.listdir("."):
    try:
        index = filename.index(key)
        _, file_extension = os.path.splitext(filename)
        new_name = filename[index:index+len(key)+3] + file_extension
        os.rename(filename, new_name)
    except ValueError:
        pass

Upvotes: 2

l'L'l
l'L'l

Reputation: 47302

If your filename is always separated by periods then split() might be sufficient:

import os

ext = ".mkv"
ndl = "Episode"

for filename in os.listdir("."):
    if ext in filename and ndl in filename:
        num = filename.split(ndl, 1)[1].split(" ")[0].split(".")[1]
        epi = "{}.{}{}".format(ndl, num, ext)
        os.rename(filename, epi)

This should split the name after your needle ("ndl") grab the episode number and rename the file; it should also handle filenames that include spaces in addition to periods or if "Episode.26" is at the end of the string (eg. Some.Movie.Episode.26 [BC6DDF99].mkv).

Result:

Episode.26.mkv

Upvotes: 2

Related Questions