def add_info_to_file(cd_dir= ''):
present_dir = os.getcwd() #记录一下当前的目录
os.chdir(cd_dir) #参数是跳转到想要的目录下
isExists = os.path.exists('XXX_bak')
if not isExists:
shutil.copyfile('XXX', 'XXX_bak') #第一次备份一下文件,以后运行脚本不需要再备份
addInfo= ' xxxxxxxxxx' # 需要新增的内容
Flag = 0
with open('XXX', 'r') as fr:
lines = fr.readlines()
with open('XXX', 'w') as fw:
for line in lines:
if 'key1' in line:
fw.write(addInfo) #如果该行有关键字1,就用新的信息替换
Flag = 1
else:
&
present_dir = os.getcwd() #记录一下当前的目录
os.chdir(cd_dir) #参数是跳转到想要的目录下
isExists = os.path.exists('XXX_bak')
if not isExists:
shutil.copyfile('XXX', 'XXX_bak') #第一次备份一下文件,以后运行脚本不需要再备份
addInfo= ' xxxxxxxxxx' # 需要新增的内容
Flag = 0
with open('XXX', 'r') as fr:
lines = fr.readlines()
with open('XXX', 'w') as fw:
for line in lines:
if 'key1' in line:
fw.write(addInfo) #如果该行有关键字1,就用新的信息替换
Flag = 1
else:
&