gpt4 book ai didi

python - 显示重复文件

转载 作者:行者123 更新时间:2023-12-01 01:50:35 24 4
gpt4 key购买 nike

我已经能够编写简单的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(或任何更好的 View )中显示 print(duplicate),并且还能够获取文件的索引

最佳答案

尝试一下:

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

关于python - 显示重复文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50756044/

24 4 0
文章推荐: javascript - Reactjs : display time in 24hr format
文章推荐: JQueryMobile 可折叠标题
文章推荐: jquery - Values 文件上传 - onComplete
文章推荐: jQuery 替换我表单中的