##################### # # Author: F. Lionetto # Date: 07/10/2014 # ''' Change the extension of all files in a given directory. Run with: python ChangeExtension.py --f <directory> --o <old extension> --n <new extension> ''' import argparse import os, sys parser = argparse.ArgumentParser(description='Define directory, old extension, and new extension.') # Define folder. parser.add_argument('--f',type=str,help='directory') # Define old extension. parser.add_argument('--o',type=str,help='old extension') # Define new extension. parser.add_argument('--n',type=str,help='new extension') args = parser.parse_args() directory = args.f for filename in os.listdir(directory) : infilename = os.path.join(directory,filename) if not os.path.isfile(infilename) : continue oldbase = os.path.splitext(filename) newname = infilename.replace(args.o,args.n) output = os.rename(infilename,newname)