修改文件中的内容,使用fileinput模块

import fileinput

filename = 'passwd'

for line in fileinput.input(filename,inplace=1,backup='.bak')

    print line.replace('root','ROOT')
a = ' this is apple'
a.replace('this','that')

对字符串的替换
原文地址:https://www.cnblogs.com/fengjian2016/p/5234250.html