python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python文件查找

Python+PyQt5来实现文件高速查找

作者:W金刚葫芦娃W

这篇文章主要为大家详细介绍了如何模拟Everything,即通过python+PyQt5来实现可视化文件的高速查找,文中的示例代码讲解详细,需要的可以参考一下

一、功能

模拟Everything,通过python+PyQt5来实现可视化文件的高速查找。

二、代码

import os
import threading
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QTextBrowser, QPushButton, QComboBox
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt, pyqtSignal, QObject
 
 
class FileSearchThread(QObject):
    search_result_found = pyqtSignal(str)
    search_finished = pyqtSignal()
 
    def __init__(self, disk, search_text):
        super().__init__()
        self.disk = disk
        self.search_text = search_text
        self.found_files = []
 
    def search_files(self):
        for root, dirs, files in os.walk(self.disk):
            for file in files:
                if self.search_text.lower() in file.lower():
                    file_path = os.path.join(root, file)
                    self.found_files.append(file_path)
                    self.search_result_found.emit(file_path)
 
        self.search_finished.emit()
 
 
class FileSearchApp(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('文件搜索')
        self.setGeometry(200, 600, 1340, 800)
 
        self.search_label = QLabel("搜索关键字:")
        self.search_label.setFont(QFont('Arial', 12))
        self.search_textbox = QLineEdit()
        self.search_textbox.setFont(QFont('Arial', 12))
 
        self.search_button = QPushButton('搜索')
        self.search_button.setFont(QFont('Arial', 12))
        self.search_button.clicked.connect(self.start_search)
 
 
        self.search_results = QTextBrowser()
        self.search_results.setFont(QFont('Arial', 12))
        self.search_status_label = QLabel()
        self.search_status_label.setFont(QFont('Arial', 12))
 
        layout = QVBoxLayout()
        layout.addWidget(self.search_label)
        layout.addWidget(self.search_textbox)
        layout.addWidget(self.search_button)
        layout.addWidget(self.search_status_label)
        layout.addWidget(self.search_results)
        self.setLayout(layout)
 
    def start_search(self,disks):
        disks= ['C:','D:','E:','F:']   #本地电脑的磁盘列表
        search_text = self.search_textbox.text()
        # print(disk)
        self.search_results.clear()
        self.search_status_label.setText("正在搜索...")
        self.search_status_label.adjustSize()
        for disk in disks:
            if disk and search_text:
                file_search_thread = FileSearchThread(disk, search_text)
                file_search_thread.search_result_found.connect(self.display_result)
                file_search_thread.search_finished.connect(self.search_finished)
 
                self.worker_thread = threading.Thread(target=file_search_thread.search_files)
                self.worker_thread.start()
 
    def display_result(self, file_path):
        self.search_results.append(file_path)
 
    def search_finished(self):
        self.search_status_label.setText("搜索完成")
        self.search_status_label.adjustSize()
        self.worker_thread.join()
 
 
if __name__ == '__main__':
    app = QApplication([])
    file_search_app = FileSearchApp()
    file_search_app.show()
    app.exec_()

三、运行结果

到此这篇关于Python+PyQt5来实现文件高速查找的文章就介绍到这了,更多相关Python文件查找内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文