#字符串替换 import sys f = open("yesterday2","r",encoding="utf-8") f_new = open("yesterday2.bak","w",encoding="utf-8") find_str = sys.argv[1] replace_str = sys.argv[2] 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() #with执行问了,文件就自动关闭了! 开发规范一行代码不能超过80字符: \
with open("yesterday2","r",encoding="utf-8") as f ,\ open("yesterday2", "r", encoding="utf-8") as f2: for line in f: print(line)