with open('1.txt', '+r',encoding='utf-8') as f: t = f.read() t = t.replace(':', ':') # 读写偏移位置移到最开始处 f.seek(0, 0) f.write(t) # 设置文件结尾 EOF f.truncate()替换符号