Skip to content

Instantly share code, notes, and snippets.

@terrettaz
Created February 12, 2011 10:08
Show Gist options
  • Save terrettaz/823664 to your computer and use it in GitHub Desktop.
Save terrettaz/823664 to your computer and use it in GitHub Desktop.
This script lets you define rules to rename your files in a specific directory.
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
__version__ = "$Revision: 0.2$"
__author__ = "Pierrick Terrettaz"
__date__ = "2006-12-09"
import os, sys, re
from optparse import OptionParser
def rename_file(path, search, replace, options, sub):
f = os.path.basename(path)
d = os.path.dirname(path)
new_name = sub(f, search, replace)
if new_name != None:
new_path = os.path.join(d, new_name)
if options.verbose or options.simulation:
print '%(path)s -> %(new_path)s' % locals()
if not options.simulation:
os.rename(path, new_path)
return 1
return 0
def rename_in_dir(d, search, replace, options, sub):
count = 0
total = 0
for f in os.listdir(d):
path = os.path.join(d, f)
if options.recursive and os.path.isdir(path):
c, t = rename_in_dir(path, search, replace, options, sub)
count += c
total += t
if options.directories_only and os.path.isfile(path):
continue
if options.files_only and os.path.isdir(path):
continue
count += rename_file(path, search, replace, options, sub)
total += 1
return count, total
def std_sub(string, search, replace):
if string.find(search) != -1:
return string.replace(search, replace)
return None
def re_sub(string, search, replace):
new_name = re.sub(search, replace, string)
if new_name != string:
return new_name
return None
if __name__ == "__main__":
parser = OptionParser()
parser.set_usage("%s directory search_string replace_string" % os.path.basename(sys.argv[0]))
parser.add_option("-r", "--recursive", action="store_true", dest="recursive", default=False, help="recursive mode")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose output")
parser.add_option("-f", "--file-only", action="store_true", dest="files_only", default=False, help="process only on files")
parser.add_option("-d", "--directory-only", action="store_true", dest="directories_only", default=False, help="process only on directories")
parser.add_option("-s", "--simulate", action="store_true", dest="simulation", default=False, help="make a simulation of the renaming")
parser.add_option("-e", "--use-regex", action="store_true", dest="regex", default=False, help="use input as regex. if regex contains groups, use \\1")
(options, args) = parser.parse_args()
if len(args) != 3:
parser.print_usage()
sys.exit(1)
(path, search, replace) = args
if path == "/":
print "WARNING: Do you really want to rename all files of root directory ? [y/n]"
if sys.stdin.readline() != "y\n":
sys.exit(0)
if options.regex:
sub = re_sub
else:
sub = std_sub
count, total = rename_in_dir(path, search, replace, options, sub)
print "%(total)d entries scanned, %(count)d entries renamed" % locals()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment