Python替换文件内容

#!/usr/bin/env python
import fileinput

for line in fileinput.input('fansik',inplace=1):
        line = line.replace('this is 0','1234')
        print line,

inplace=1相当于sed的-i参数,如果不加inplace=1,则只答应修改内容,不修改文件

#!/usr/bin/env python
import fileinput

for line in fileinput.input('fansik', inplace=1, backup='.bak'):
        line = line.replace('this is 1','1234')
        print line,

backup参数为修改之前备份一下源文件

 

posted @ 2017-06-28 16:35  fansik  阅读(353)  评论(0编辑  收藏  举报