import os
class ReplaceFileKeywords:
def __init__(self, template_path, old_keyword, new_keyword):
self.template_path = template_path
self.old_keyword = old_keyword
self.new_keyword = new_keyword
def replaceContentKeywords(self, content=''):
content = content if content else self.template_path
old_keyword = self.old_keyword
new_keyword = self.new_keyword
return content.replace(old_keyword, new_keyword)\
.replace(old_keyword.capitalize(), new_keyword.capitalize())\
.replace(old_keyword.upper(), new_keyword.upper())
def handlePath(self, template_path=''):
# test_path = 'E:\phpStudy\WWW\local.python.com\HelloWorld'
template_path = template_path if template_path else self.template_path
new_payment_path = self.replaceContentKeywords(template_path)
if not os.path.isdir(new_payment_path):
os.makedirs(new_payment_path)
for filename in os.listdir(template_path):
cur_path = template_path + '/' + filename
if os.path.isdir(cur_path):
# print('-'*20)
# print(cur_path, 'is dir.')
self.handlePath(cur_path)
else:
file_handle = open(cur_path, 'r')
new_file = new_payment_path + '/' + self.replaceContentKeywords(filename)
with open(cur_path, 'r') as file_handle:
file_content = file_handle.read()
new_content = self.replaceContentKeywords(file_content)
with open(new_file, 'w') as new_file_handle:
new_file_handle.write(new_content)
# print(cur_path, 'is file')
template_path = 'E:\phpStudy\WWW\local.python.com\payment\stripe'
old_payment_keyword = 'stripe'
new_payment_keyword = 'yipay'
obj = ReplaceFileKeywords(template_path, old_payment_keyword, new_payment_keyword)
obj.handlePath()
# handlePath(template_path, old_payment_keyword, new_payment_keyword)
print('finish')