美文网首页
python遍历文件并替换

python遍历文件并替换

作者: 合拍v | 来源:发表于2023-02-19 19:52 被阅读0次

有时候碰到需要批量替换文件中的字符串,用比较简短的python代码操作一下。

import os

filepath = '...'
lines = open(filepath).readlines()
fp = open(filepath,'w')
for s in lines:
    fp.write( s.replace('orginString', 'targetString'))
fp.close()

相关文章

网友评论

      本文标题:python遍历文件并替换

      本文链接:https://www.haomeiwen.com/subject/drfwkdtx.html