26.Python:文件修改的两种方式
2021-06-28 14:04
标签:imp 修改 move 文件 encoding res mode txt mod # 方式二 with open(‘a.txt‘, mode=‘rt‘, encoding=‘utf-8‘) as f1, \ os.remove(‘a.txt‘) 26.Python:文件修改的两种方式 标签:imp 修改 move 文件 encoding res mode txt mod 原文地址:https://www.cnblogs.com/wyless/p/14930572.html
# 方式一:文本编辑器的方式
# with open(‘a.txt‘, mode=‘rt‘, encoding=‘utf-8‘) as f1:
# res = f1.read()
# data = res.replace(‘a1‘, ‘b1‘)
#
# with open(‘a.txt‘, mode=‘wt‘, encoding=‘utf-8‘) as f2:
# f2.write(data)
import os
open(‘b.txt‘, mode=‘wt‘, encoding=‘utf-8‘) as f2:
for line in f1:
f2.write(line.replace(‘a1‘, ‘b1‘))
os.rename(‘b.txt‘, ‘a.txt‘)
文章标题:26.Python:文件修改的两种方式
文章链接:http://soscw.com/index.php/essay/98919.html