批量替换文件关键字


# -*- coding: UTF-8 -*-

from __future__ import unicode_literals
import os,sys,re

"""默认对象列表"""
alltenants = ['4600', '4601', '4602', '4603', '4606', '4607', '4608', '4630', '4660', '4666']

dirnow = os.getcwd()
dirin = dirnow + '/' + 'infile'
dirout = dirnow + '/' + 'outfile'

# 创建脚本文件输入输出目录
def findPath(dirin,dirout):
if not os.path.exists(dirin) and not os.path.exists(dirout):
os.mkdir(dirout)
# print('********** 脚本输出目录outfile创建成功 **********')
print('********** Directory outfile created success **********')
os.mkdir(dirin)
# print('********** 创建infile目录成功,请手动将源脚本放入infile目录 **********')
print('********** Directory infile created,Pls put sql files into this directory **********')
input('Press <Enter>')
findPath(dirin,dirout)


def edit(newfile, sqlall,tenantid, tenantmore):
# newfile 脚本文件名称
# sqlall 脚本中语句
# tenantid 当前租户id
# tenantmore 目标租户id
print('********** excuting ' + newfile)
opnewfile = open(newfile, 'a+', encoding='utf-8')
with opnewfile:
newrow = sqlall.replace(tenantid, tenantmore)
opnewfile.write('\n' + newrow)
# for sqlrow in sqlall:
# newrow = sqlrow.replace(tenantid, tenantmore)
# opnewfile.write('\n' + newrow)
opnewfile.close()
# print('脚本输出到outfile目录完成')

def tenantsSqls(tenantid,alltenants,newfile,sqlall):
if os.path.isfile(newfile):
print('********** [ERROR] Same filename in dest directory', newfile)
ck = input('********** Cover same file? (Y/N): ')
if ck[0].lower() == 'n':
print('********** Ignore same name file **********')
else:
os.remove(newfile)
for tenantmore in alltenants:
# newname = filename.replace(tenantid, tenantmore)
# newpath = dirout + "/" + newname
edit(newfile, sqlall, tenantid, tenantmore)
else:
print('********** Start checkout tenants **********')
for tenantmore in alltenants:
# newname = filename.replace(tenantid, tenantmore)
# newpath = dirout + "/" + newname
edit(newfile, sqlall,tenantid, tenantmore)

if __name__ == '__main__':
fileall = os.listdir(dirin)
for file in fileall:
if os.path.isdir(file):
print(file + '********** This is not sql file **********')
else:
if not file:
print('********** [ERROR] Put files in folder "infile" and play again **********')
sys.exit(0)
else:
filename = file
newfile = dirout + "/" + filename
oldfile = dirin + "/" + filename

sqlfile = open(oldfile, 'r', encoding='utf-8')
"""判断脚本文件编码是否utf-8"""
with sqlfile as f1:
try:
sqlall = f1.read()
except BaseException as exc:
print("********** [ERROR] Maybe codestyle wrong in the file ********** ")
print(filename)
print(exc)
sqlfile.close()

print(newfile)
select = input("********** Do with all tenants? (Y/N/S): ")

if select[0].lower() == 'y':
if re.match('8600', filename, flags=0):
print('********** ' + filename + ' **********')
tenantsSqls('8600', alltenants,newfile,sqlall)
else:
print("********** [WARN] Ignore this file: " + filename)
elif select[0].lower() == 'n':
tenant_now = input("********** Which word to replace? (ex:8600): ")
tenant_code = input("********** Input target replace to(use <,> to split): ")
tenant_code = tenant_code.split(',')
alltenants = []
for code in tenant_code:
alltenants.append(code)
tenantsSqls(tenant_now, alltenants,newfile,sqlall)
elif select[0].lower() == 's':
opnewfile = open(newfile, 'a+', encoding='utf-8')
with opnewfile:
newrow = sqlall
opnewfile.write('\n' + newrow)
opnewfile.close()

print( '=' * 60 )
print('Done.Thank you for using')
input('Press <Enter>')