Показать дубликаты файлов - PullRequest
0 голосов
/ 08 июня 2018

я смог написать простые скрипты 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 отображал print(duplicate) в QListView (или любом другом лучшем представлении), а также имел возможность получать индекс для файлов

1 Ответ

0 голосов
/ 08 июня 2018

Попробуйте:

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...