#!/usr/bin/python # -*- coding: utf-8 -*- # $Id: serialrename.py,v 1.5 2007/11/27 15:05:06 thierry Exp $ #commentary: #Rename the content of a directory #+with incremental numbers #+ for a specific extension file import os from optparse import OptionParser import sys parser = OptionParser() parser.add_option("-d", "--dir", dest="rep", help ="Input directory(with / at the end please)") parser.add_option("-n", "--name", dest="nom", default="file", help ="optional:file name") parser.add_option("-s", "--start-number", dest="startn", default=101, help ="optional:initial incremental number") parser.add_option("-e", "--ext", dest="extension", help ="file type - ex: jpg(without dot)") (options, args) = parser.parse_args() rep = options.rep nom = options.nom startn = int(options.startn) extension = options.extension if len(sys.argv) < 3: parser.error("Serialrename take at list 2 arguments\n you must specify options -d and -e") else: try: dir_photos = os.listdir(rep) for i in dir_photos: if extension in i: new = rep + nom + str(startn) + "." + extension if new not in dir_photos: print new os.rename(rep + i, new) startn += 1 else: print new, "new" os.rename(rep + i, new + "new") startn += 1 except (IOError, TypeError): raise "Check the path of your dir - Have you forget the / at the end ?"