用python 替换文件中的git地址

有个需求要替换文件中git地址,要替换成的git地址是一个变量

本来想用sed替换但是git地址中有斜杠符号 需要转义,提前知道还好弄,如果是变量就不好处理了

#!/usr/bin/python3
# -*- coding: utf-8 -*-

#替换git地址
import os
git_url = os.environ.get('git_url')
git_old = os.environ.get('git_old')

f = open('/tmp/oc_export.json','w',encoding='utf-8')
f_bak = open('/tmp/oc_export.json.bak','r',encoding='utf-8')
for line in f_bak:
    line = line.replace(git_old,git_url)
    f.write(line)

猜你喜欢

转载自www.cnblogs.com/37yan/p/8932881.html