python 3文件内容替换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 3文件内容替换相关的知识,希望对你有一定的参考价值。

在原有的文件上,修改替换,并放到新的文件里

old = open(‘day‘,‘r‘)
new = open(‘new_day‘,‘w‘)
old_replace = input(‘please replace content:‘)
new_replace = input(‘please new replace content:‘)

for i in old:
    if old_replace in i:
        i = i.replace(old_replace,new_replace)
    new.write(i)

old.close()
new.close()

以上是关于python 3文件内容替换的主要内容,如果未能解决你的问题,请参考以下文章