实现简单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)