16 Aug 2016
希望把a.txt中的所有”hello”字符串替换成为”byebye”
其实使用shell的sed更方便,一条命令的事情,这里仅就函数的运用做锻炼
脚本使用函数
open 打开文件os.path.isfile 判断文件是否存在os.remove 删除文件str.replace 替换字符串内容import os with open('a.txt', 'r') as f: if os.path.isfile('a.replace'): os.remove('a.replace') with open('a.replace', 'a') as f_r: for line in f.readlines(): if 'hello' in line: line = line.replace('hello', 'byebye') f_r.write(line)
python str_replace.py cat a.txt | grep world | head hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world cat a.replace | grep world | head byebye world byebye world byebye world byebye world byebye world byebye world byebye world byebye world byebye world byebye world