程序:实现简单的shell sed替换功能
#实现简单的shell sed替换功能,保存为file_sed.py
#打开命令行输入python file_sed.py 我 Alex,回车后会把文件中的“我”全部替换为“Alex”
import sys
find_str=sys.argv[1]
replace_str=sys.argv[2] f=open("yesterday.txt",'r',encoding='UTF-8')
f_new=open("yesterday.bak",'w',encoding='UTF-8') for line in f:
if find_str in line:
line=line.replace(find_str,replace_str)
f_new.write(line) f.close()
f_new.close()