mancha 发表于 2018-8-11 12:08:05

python 实现类似sed命令的文件内容替换

#!/usr/bin/env python  
#_*_coding:utf-8 _*_
  
#replace()方法把字符串中的 old(旧字符串)替换成new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
  
#语法:str.replace(old, new[, max])
  
import sys,os
  
old_text, new_text, file_name = sys.argv, sys.argv, sys.argv
  
f = file(file_name,'rb')
  
new_file = file('%s.bak' % file_name,'wb')
  
for line in f.xreadlines():
  
new_file.write(line.replace(old_text,new_text))
  
f.close()
  
new_file.close()
页: [1]
查看完整版本: python 实现类似sed命令的文件内容替换