且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Python搜索和替换文件中的文本?

更新时间:2023-02-23 14:50:06

fileinput already supports inplace editing. It redirects stdout to the file in this case:

#!/usr/bin/env python3
import fileinput

with fileinput.FileInput(fileToSearch, inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace(textToSearch, textToReplace), end='')

上一篇 : :如何使用bat文件脚本替换文本文件中的文本?下一篇 : Firestore不会保存文本区域中的换行符

相关阅读

技术问答最新文章