import os
from optparse import OptionParser, Option
class MyOption (Option):
ACTIONS = Option.ACTIONS + ("extend",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
def take_action(self, action, dest, opt, value, values, parser):
if action == "extend":
lvalue = value.split(",")
values.ensure_value(dest, []).extend(lvalue)
else:
Option.take_action(
self, action, dest, opt, value, values, parser)
def parse_opt():
parser = OptionParser(option_class=MyOption)
parser.add_option("-r", "--replace", action="extend", type='string', dest='replacename',
help = 'type in replace file name here ex: replacing .txt with .py: -r .txt,.py')
return parser.parse_args()
def filenamechanger():
# print('Hello World')
(options, args) = parse_opt()
# print('hello world')
ticker = 0
for filename in os.listdir("."):
# print(filename)
if filename.count(str(options.replacename[0])):
newfilename = str(filename).replace(str(options.replacename[0]),str(options.replacename[1]))
print('Changing file %s to %s'%(filename,newfilename))
os.rename(filename, newfilename)
ticker += 1
print ("Changed the name of %s files! Nice!"%ticker)
if __name__ == "__main__":
filenamechanger()
print ("File Name Changer End...............")