程序:实现简单的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()
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-11-23
  • 2021-07-07
  • 2021-06-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
  • 2022-01-19
  • 2022-03-04
相关资源
相似解决方案