Skip to content

Instantly share code, notes, and snippets.

@theand
Created May 14, 2014 03:45
Show Gist options
  • Save theand/80d4207fc6dd4f730bdd to your computer and use it in GitHub Desktop.
Save theand/80d4207fc6dd4f730bdd to your computer and use it in GitHub Desktop.
rename a bunch of mp4 files to specified naming pattern.
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os
import os.path
import shutil
import re
FILENAME = re.compile("^(?P<number>\d\d)_(?P<title>.*)-(?P<name>[a-z]*-[a-z]*)")
for dirpath, dirnames, filenames in os.walk('.'):
for filename in filenames:
if filename.endswith(".py") or filename.startswith('.'):
continue
if not filename.lower().endswith('.mp4'):
continue
full_src_path = os.path.join(dirpath, filename)
if not os.path.exists(full_src_path):
print 'no exist : ' + full_src_path
continue
if not os.path.isfile(full_src_path):
print 'not file : ' + full_src_path
continue
name, ext = os.path.splitext(filename)
r = FILENAME.search(name)
if not r:
print "not match : " + name
continue
result = r.groupdict()
number = result['number']
title = result['title']
person = result['name']
dest = "%s - %s - %s.mp4" % (number, title.replace('-', ' ').title(), person.replace('-', ' ').title())
full_dest_path = os.path.join(dirpath, dest)
#print full_dest_path
shutil.move(full_src_path, full_dest_path)
print 'move - ' + dest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment