修改源文件的内容到新文件
1、写死要替换的内容
__Author__ = "Devin" f1 = open("song",'r',encoding='utf-8') f2 = open("song.bak",'w',encoding='utf-8') for line in f1: if "寻找一个我属于的地方" in line: line = line.replace("寻找一个我属于的地方","寻找一个*****devin*****属于的地方") f2.write(line) f1.close() f2.close()
2、根据传递的参数[1]、[2]修改内容
__Author__ = "Devin" import sys f1 = open("song",'r',encoding='utf-8') f2 = open("song.bak",'w',encoding='utf-8') find_str = sys.argv[1] replace_str = sys.argv[2] for line in f1: if find_str in line: line = line.replace(find_str,replace_str) f2.write(line) f1.close() f2.close()
It is the quality of one's convictions that determines success, not the number of followers.

浙公网安备 33010602011771号