PythonFor Days
PythonFor Days

Reputation: 201

Rename files with Python

I am trying to use python to rename over 1000 files in a windows directory that have this naming convention:

The.Woman.In.Black.2012.720p.BluRay.x264

The desired naming convention is: The Woman In Black {2012}

The only important things I need from the original name are the title and date....

I have tried using different variations of string manipulation and I am not getting anywhere. I need help getting started.

I understand I should use the os module to rename files, but what procedure should I try and use to do this task?

Any help would be greatly appreciated. Thanks.

Upvotes: 0

Views: 2239

Answers (4)

Volatility
Volatility

Reputation: 32300

def change_filename(name):
    filename = name.split('.')
    wordlist = filename[:-4]
    extension = '.' + filename[-1]
    wordlist[-1] = '{' + wordlist[-1] + '}'
    os.rename(name, ' '.join(wordlist) + extension)

This function should do exactly what you want, you just need to feed it the names.

Note: you probably want to add a file extension on to the filename, otherwise it might go wonky and everything.

If you run this on the same file more than once, you'll get a file overloaded with braces, and you don't want that, so here's a reversal function:

def filename_cleanup(name):
    filename = name.replace('{', '').replace('}', '')
    wordlist = filename.split(' ')
    extension = '.' + wordlist.pop()
    wordlist[-1] = '{' + wordlist[-1] + '}'
    os.rename(name, ' '.join(wordlist) + extension)

That should give you the proper filename again.

Upvotes: 0

poke
poke

Reputation: 387507

filename = 'The.Woman.In.Black.2012.720p.BluRay.x264'

# using regular expressions
import re
title, year = re.match('(.*?)\.(\d{4})\.720p\.BluRay\.x264', filename).groups()

# simpler regular expression, matching any format specifiers
title, year = re.match('(.+)\.(\d{4})\.', filename).groups()

# using simple string manipulation if the suffix is always the same
title, year = filename[:-22], filename[-21:-17]

# normalize title
title = title.replace('.', ' ')

# build new filename
print('%s {%s}' % (title, year)) # The Woman In Black {2012}

To rename files, use os.rename; to iterate through a directory, use glob.glob or os.walk.

Upvotes: 3

Ethan Furman
Ethan Furman

Reputation: 69001

It looks like the general format of your titles is:

name.name.name.name.name.date.resolution.format.codec

in which case I would do this (not tested):

import os

TARGET_DIR = r'/path/to/movies/'

for old_title in os.listdir(TARGET_DIR + '*'):
    words = old_title.split('.')
    date = words[-4]
    name = ' '.join(words[:-4])
    new_title = '%s {%s}' % (name, date)
    old_title = os.path.join(TARGET_DIR, old_title)
    new_title = os.path.join(TARGET_DIR, new_title)
    os.rename(old_title, new_title)

Upvotes: 1

WildCrustacean
WildCrustacean

Reputation: 5966

If the original naming convention is consistent, you could split the original filename string on the '.' character, and then look for a valid year, add the braces, and join it with all the preceding tokens to form the new name.

See this question for how to rename a file using python.

Upvotes: 0

Related Questions