python3 对文件的查找、替换、删除
python 版本 3.5
实现对文件的查找,替换,删除
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#Author by Andy #_*_ coding:utf-8 _*_ #定义查找函数 def find(): Keywords=input('请输入关建字:') for i in f.readlines(): if Keywords in i: print(i) f.close() #定义替换函数 def change(): old=input('请输入替换前的内容:') new=input('请输入替换后的内容:') for i in f.readlines(): if old in i: a = i[:i.find(old)] b = i[i.find(old) + len(old):] print(a, new, b) f.close() ##################################################### print("Welcome to use this program!") print("操作提示:\n" "查找按F\n" "替换按C") print("现在请输入您要操作的文件的全路径!") File_path=input(':') f=open(File_path,encoding='utf-8') #函数主体 while True: f = open(File_path, encoding='utf-8') Command = input("请选择您要执行的操作:") if Command == 'F'or Command == 'f': find() elif Command == 'C'or Command == 'c': change() elif Command == 'q' or Command == 'Q': print("谢谢使用,再见!") exit() f.close() else: print("Invalable Options") continue
转载请注明出处!