实现简单sed替换功能的python脚本

###可实现修改配置文件参数信息

#!/usr/bin/env python3

# -*- coding: utf-8 -*-


import sys,os

oldstr = sys.argv[1]
newstr = sys.argv[2]
filename = sys.argv[3]
filename2 = "file2"

f = open(filename, "r")
f2 = open(filename2, "r+")
for line in f:
    if oldstr in line:
        line = line.replace(oldstr, newstr)
        f2.write(line)
        f2.flush()
    f2.write(line)
    f2.flush()

f.close()
f2.close()

os.remove(filename)
os.rename(filename2, filename)

 

posted on 2018-03-06 10:53  李永山  阅读(97)  评论(0编辑  收藏  举报

导航