python3 對文件的查找、替換、刪除


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
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM