python仿evething的文件搜索器實(shí)例代碼
今天看到everything搜索速度秒殺windows自帶的文件管理器,所以特地模仿everything實(shí)現(xiàn)了文件搜索以及打開對(duì)應(yīng)文件的功能,首先來(lái)一張搜索對(duì)比圖。
這是evething搜索效果:
這是自己實(shí)現(xiàn)的效果:
主要功能就是python的os庫(kù)的文件列表功能,sqllite創(chuàng)建表,插入數(shù)據(jù)以及模糊搜索,然后就是tkiner實(shí)現(xiàn)的界面功能。全部代碼貼出來(lái)做一次記錄,花費(fèi)一天時(shí)間踩坑。
# coding=utf-8 import tkinter as tk import tkinter.messagebox #這個(gè)是消息框,對(duì)話框的關(guān)鍵 import tkinter.constants import sqlite3 import os import threading import traceback def update_db(): print("更新數(shù)據(jù)庫(kù)") tkinter.messagebox.showerror("錯(cuò)誤提示","更新數(shù)據(jù)庫(kù)功能未完待續(xù),可以刪除目錄下的allFiles.db文件,然后點(diǎn)擊搜索,即可刷新數(shù)據(jù)庫(kù)") def mouseCallBack(*args): indexs = listb.curselection() index = int(indexs[0]) print("index",index) start_directory = str(myArr[index]) print(start_directory[2:-3]) os.startfile(start_directory[2:-3]) def obtain_all_files(filepath,cursor): #遍歷filepath下所有文件,包括子目錄 try: files = os.listdir(filepath) for fi in files: fi_d = os.path.join(filepath,fi) if os.path.isdir(fi_d): obtain_all_files(fi_d,cursor) else: path = os.path.join(filepath,fi_d) update_progress.set(path) print("目錄",path) sqlAdd = "insert into filepath (file_path) values ('"+path+"')" print("sqlAdd",sqlAdd) cursor.execute(sqlAdd) except Exception as e: traceback.print_exc() print("掃描文件出異常了,點(diǎn)擊確定繼續(xù)掃描") tkinter.messagebox.showerror("錯(cuò)誤提示","掃描文件出異常了看,點(diǎn)擊確定繼續(xù)掃描") def scan_file(): print("開始掃描文件") # del myArr[:] connection.execute("BEGIN TRANSACTION;") # 關(guān)鍵點(diǎn) cursor = connection.cursor() obtain_all_files('G:\\',cursor) print("G盤掃描完成...") tkinter.messagebox.showinfo("溫馨提示","G盤掃描完成....") connection.execute("COMMIT;") #關(guān)鍵點(diǎn) connection.commit() connection.close() def insert_db(): t1 = threading.Thread(target=scan_file) t1.setDaemon(True) t1.start() tkinter.messagebox.showinfo("溫馨提示","正在更新數(shù)據(jù)庫(kù),請(qǐng)等待...") def search_file(): #表示創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),并獲得連接 print("數(shù)據(jù)庫(kù)是否存在: ",isExistDB) if(isExistDB==False): tkinter.messagebox.showwarning("警告","數(shù)據(jù)庫(kù)不存在,將更新數(shù)據(jù)庫(kù)文件!") try: mycursor = connection.cursor() file_sql = "create table filepath('file_path' text not null)" mycursor.execute(file_sql) mycursor.close() insert_db() except: tkinter.messagebox.showerror("錯(cuò)誤提示","數(shù)據(jù)庫(kù)發(fā)生異常...") return else: print("開始搜索") listb.delete(0,tk.constants.END) mycursor = connection.cursor() entry_text = inputText.get() search_sql = "select * from filepath where file_path like '%"+entry_text+"%'" files = mycursor.execute(search_sql) #tkinter.messagebox.showwarning("警告","沒(méi)有找到對(duì)應(yīng)的文件!") for f in files: print(f) myArr.append(f) listb.insert(tkinter.constants.END,f) print("搜索完成") mycursor.close() myArr = [] isExistDB = os.path.exists("allFiles.db") connection = sqlite3.connect("allFiles.db",check_same_thread = False) root = tk.Tk() # 初始化Tk() root.title("電腦文件搜索工具(仿everything)By景兄弟V1.0") # 設(shè)置窗口標(biāo)題 root.geometry("800x600") # 設(shè)置窗口大小 注意:是x 不是* root.resizable(width=False, height=False) # 設(shè)置窗口是否可以變化長(zhǎng)/寬,F(xiàn)alse不可變,True可變,默認(rèn)為True #設(shè)置輸入框 inputText = tk.Entry(root,show=None,foreground = 'red',font = ('Helvetica', '15', 'bold'),insertbackground = 'green',width=65) inputText.pack() #設(shè)置按鈕,以及放置的位置 searchBtn = tk.Button(root, text="搜索", fg="blue",bd=2,width=10,command=search_file)#command中的方法帶括號(hào)是直接執(zhí)行,不帶括號(hào)才是點(diǎn)擊執(zhí)行 searchBtn.place(x=200, y=40, anchor='nw') updateBtn = tk.Button(root, text="更新數(shù)據(jù)庫(kù)", fg="blue",bd=2,width=10,command=update_db) updateBtn.place(x=400, y=40, anchor='nw') update_progress = tk.StringVar() update_progress.set('還未開始掃描') lb = tk.Label(root,text="還未開始", fg="blue",bd=2,width=100, textvariable=update_progress) lb.place(x=20,y=90) listb = tk.Listbox(root,width=110,height=20) listb.place(x=1, y=120, anchor='nw') sb = tk.Scrollbar(root) #垂直滾動(dòng)條組件 sb.pack(side=tkinter.constants.RIGHT,fill=tkinter.constants.Y) #設(shè)置垂直滾動(dòng)條顯示的位置 listb.config(yscrollcommand=sb.set) listb.bind("<<ListboxSelect>>",mouseCallBack) root.mainloop() # 進(jìn)入消息循環(huán)
以上所述是小編給大家介紹的python仿evething的文件搜索器詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 在Python的Flask框架中實(shí)現(xiàn)全文搜索功能
- python搜索算法原理及實(shí)例講解
- Python大批量搜索引擎圖像爬蟲工具詳解
- Python利用Faiss庫(kù)實(shí)現(xiàn)ANN近鄰搜索的方法詳解
- OpenCV python sklearn隨機(jī)超參數(shù)搜索的實(shí)現(xiàn)
- Python實(shí)現(xiàn)搜索算法的實(shí)例代碼
- Python實(shí)現(xiàn)二叉搜索樹BST的方法示例
- python搜索包的路徑的實(shí)現(xiàn)方法
- python+selenium實(shí)現(xiàn)自動(dòng)化百度搜索關(guān)鍵詞
- Python實(shí)現(xiàn)中英文全文搜索的示例
相關(guān)文章
用python給csv里的數(shù)據(jù)排序的具體代碼
在本文里小編給大家分享的是關(guān)于用python給csv里的數(shù)據(jù)排序的具體代碼內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-07-07使用Django Form解決表單數(shù)據(jù)無(wú)法動(dòng)態(tài)刷新的兩種方法
這篇文章主要介紹了使用Django Form解決表單數(shù)據(jù)無(wú)法動(dòng)態(tài)刷新的兩種方法,需要的朋友可以參考下2017-07-07Python對(duì)象循環(huán)引用垃圾回收算法詳情
這篇文章主要介紹了Python對(duì)象循環(huán)引用垃圾回收算法詳情,文章圍繞主題展開詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-09-09OpenCV半小時(shí)掌握基本操作之圖像基礎(chǔ)操作
這篇文章主要介紹了OpenCV基本操作之圖像基礎(chǔ)操作,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Pytorch中transforms.Resize()的簡(jiǎn)單使用
這篇文章主要介紹了Pytorch中transforms.Resize()的簡(jiǎn)單使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Python+MediaPipe實(shí)現(xiàn)檢測(cè)人臉功能詳解
MediaPipe是用于構(gòu)建多模態(tài)(例如視頻、音頻或任何時(shí)間序列數(shù)據(jù))、跨平臺(tái)(即eAndroid、IOS、web、邊緣設(shè)備)應(yīng)用ML管道的框架。本文將利用MediaPipe實(shí)現(xiàn)檢測(cè)人臉功能,需要的可以參考一下2022-02-02Python實(shí)現(xiàn)循環(huán)語(yǔ)句的方式分享
這篇文章主要為大家詳細(xì)介紹了Python中實(shí)現(xiàn)循環(huán)語(yǔ)句的常用方式,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-05-05python實(shí)現(xiàn)將excel文件轉(zhuǎn)化成CSV格式
下面小編就為大家分享一篇python實(shí)現(xiàn)將excel文件轉(zhuǎn)化成CSV格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Pycharm安裝scrapy及初始化爬蟲項(xiàng)目的完整步驟
因?yàn)槿腴Tpython以來(lái)一直使用pycharm,所以對(duì)著黑白的DOS不習(xí)慣,所以此次來(lái)實(shí)現(xiàn)使用pycharm進(jìn)行實(shí)現(xiàn)使用scrapy框架,下面這篇文章主要給大家介紹了關(guān)于Pycharm安裝scrapy及初始化爬蟲項(xiàng)目的完整步驟,需要的朋友可以參考下2022-08-08