用于显示重复文件-PyQt5的GUI代码

时间:2018-06-08 07:59:55

标签: python-3.x pyqt5

我已经能够编写简单的python脚本来扫描和显示目录中的重复文件,但我已经尝试但似乎无法将其实现为GUI代码 PyQt 这是代码

def chunk_reader(fobj, chunk_size=1024):
    """Generator that reads a file in chunks of bytes"""
    while True:
        chunk = fobj.read(chunk_size)
        if not chunk:
            return
        yield chunk

def check_for_duplicates(path, hash=hashlib.sha1):
    hashes = {}
    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            full_path = os.path.join(dirpath, filename)
            hashobj = hash()
            for chunk in chunk_reader(open(full_path, 'rb')):
                hashobj.update(chunk)
            file_id = (hashobj.digest(), os.path.getsize(full_path))
            duplicate = hashes.get(file_id, None)
            if duplicate:
                print(duplicate)
                print(' done...')
            else:
                hashes[file_id] = full_path


if __name__ == '__main__':
    check_for_duplicates(ypur_path)

我希望GUI代码在QListView(或任何更好的视图)中显示print(duplicate),并且还能够获取文件的索引

1 个答案:

答案 0 :(得分:2)

试一试:

import sys
import os
import hashlib 
import traceback 

from PyQt5.QtWidgets import *
from PyQt5.QtGui     import *
from PyQt5.QtCore    import *

def chunk_reader(fobj, chunk_size=1024):
    """ Generator that reads a file in chunks of bytes """
    while True:
        chunk = fobj.read(chunk_size)
        if not chunk:
            return
        yield chunk

class SignalHelper(QObject):
    error    = pyqtSignal(tuple)
    result   = pyqtSignal(object)
    finished = pyqtSignal()
    progress = pyqtSignal(str, str)  

class Worker(QRunnable):
    def __init__(self, fn, *args, **kwargs):
        super(Worker, self).__init__()

        self.fn      = fn
        self.args    = args
        self.kwargs  = kwargs
        self.signals = SignalHelper()

        # Add a callback to our kwargs
        kwargs['progress_callback'] = self.signals.progress

    @pyqtSlot()
    def run(self):
        try:
            result = self.fn(*self.args, **self.kwargs)
        except:
            traceback.print_exc()
            exctype, value = sys.exc_info()[:2]
            self.signals.error.emit((exctype, value,  
                                    traceback.format_exc()))
        else:
            self.signals.result.emit(result)
        finally:
            self.signals.finished.emit()

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        layout = QVBoxLayout()
        self.textEdit = QTextEdit("Display duplicate files :")
        self.textEdit.setReadOnly(True)

        self.b = QPushButton("Scan files")
        self.b.setCheckable(True)
        self.b.pressed.connect(self.watcher)

        layout.addWidget(self.textEdit) 
        layout.addWidget(self.b)

        w = QWidget()
        w.setLayout(layout)
        self.setCentralWidget(w)

        self.threadpool = QThreadPool()

    def watcher(self):
        worker = Worker(self.check_for_duplicates) 
        worker.signals.progress.connect(self.progress_fn)
        self.threadpool.start(worker)

    def progress_fn(self, duplicate, full_path):
        self.textEdit.append("\nduplicate {}".format(duplicate))
        self.textEdit.append(" done...  {}".format(full_path))

    def check_for_duplicates(self, progress_callback, hash=hashlib.sha1):
        path  = "E:/_Qt/Python-Examples/_PyQt5/Temp/"           # specify your path !!!    
        hashes = {}
        for dirpath, dirnames, filenames in os.walk(path):
            for filename in filenames:
                full_path = os.path.join(dirpath, filename)
                hashobj = hash()
                for chunk in chunk_reader(open(full_path, 'rb')):
                    hashobj.update(chunk)
                file_id = (hashobj.digest(), os.path.getsize(full_path))
                duplicate = hashes.get(file_id, None)
                if duplicate:
                    progress_callback.emit(duplicate, full_path)  
                    print("\nduplicate", duplicate)
                    print(' done... ', full_path)
                else:
                    hashes[file_id] = full_path    


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.setGeometry(600, 100, 450, 200)
    window.show()
    sys.exit(app.exec_())

enter image description here