修改文件中的内容,使用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') 对字符串的替换
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') 对字符串的替换