标签:remove 开头 lines style replace 重命名 重要 读取 bytes
import os with open("?娃娃", mode="r", encoding="utf-8") as f1, open("?娃娃_new", mode="w", encoding="UTF-8") as f2: content = f1.read() new_content = content.replace("冰糖葫芦", "??梨") f2.write(new_content) os.remove("?娃娃") # 删除源?件 os.rename("?娃娃_new", "?娃娃") # 重命名新?件
弊端: ?次将所有内容进行读取. 内存溢出. 解决方案: 一行一行的读取和操作
import os with open("?娃娃", mode="r", encoding="utf-8") as f1, open("?娃娃_new", mode="w", encoding="UTF-8") as f2: for line in f1: new_line = line.replace("??梨", "冰糖葫芦") f2.write(new_line) os.remove("?娃娃") # 删除源?件 os.rename("?娃娃_new", "?娃娃") # 重命名新?件
标签:remove 开头 lines style replace 重命名 重要 读取 bytes
原文地址:https://www.cnblogs.com/wangm-0824/p/10073722.html