python 版本 3.5
實現對文件的查找,替換,刪除
#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
