import sys
"""
Usages:
1. command line: python replacestr.py para1 para2 para3;
2. para1 : file_name
para2 : original string
para3 : new string
"""
file_name = sys.argv[1]
find_str = sys.argv[2]
replace_str = sys.argv[3]
f=""
with open(file_name, "r") as fs1:
for n in fs1:
f += n.replace(find_str, replace_str)
with open(file_name, "w") as fs2:
fs2.write(f)
fs2.flush()
自定义replace函数,模仿shell的sed替换
原文:https://www.cnblogs.com/brace2011/p/9191707.html