# filePadding.py
#
# Sean Dooley
# 2/10/12
# www.smdooley.com
#
# assumes that periods (.) are only used to seperate
# file name, frame number, and file extension
import os, sys
def filePadding(path, padding):
dirlist = os.listdir(path) # lists files in path
print 'Renaming files in: %s' % path
for fname in dirlist: # loop through dir at path
root, frame, ext = fname.split('.') # unpack file name
if padding == 3:
paddedname = '%s.%03d.%s' % (root, int(frame), ext)
else:
paddedname = '%s.%04d.%s' % (root, int(frame), ext)
src = '%s\\%s' % (path, fname)
dest = '%s\\%s' % (path, paddedname)
os.rename(src, dest)
if __name__ == "__main__":
args = sys.argv[1:] # read in argument from script call
if len(args) != 2:
path = raw_input("Enter directory to rename files in: ")
padding = raw_input("Enter padding number (3 or 4): ")
else:
path = args[0] # first argument is file path to rename files in
padding = args[1] # second argument is number padding (3 or 4)
# clean up path - removes any quotes
path = path.strip('"')
path = path.strip("'")
if path != '' and padding != '':
filePadding(path, int(padding))
else:
print '\n----- Error: Need a path and padding number -----'