4.实现简单的shell sed替换功能

it2024-11-28  19

1 # -*- coding:utf-8 -*- 2 # Author: JACK ZHAO 3 4 # 程序1: 实现简单的shell sed替换功能 5 6 import sys 7 8 #判断参数个数 9 if len(sys.argv) != 3: 10 print("Usage: python_sed.py 'old string' 'net string'") 11 exit() 12 #读取所有行至内存中 13 with open("yesterday","r",encoding='utf-8') as f: 14 lines = f.readlines() 15 16 #重新生成文件yesterday,读取内存中内容至新文件 17 with open("yesterday","w",encoding='utf-8') as f: 18 for line in lines: 19 if sys.argv[1] in line: 20 line = line.replace(sys.argv[1],sys.argv[2]) #sys.argv[1]:要替换的内容 sys.argv[2]:替换后的内容 21 f.write(line)

 

转载于:https://www.cnblogs.com/changmingzhao/p/7844767.html

相关资源:数据结构—成绩单生成器
最新回复(0)