Created
July 12, 2024 03:07
-
-
Save travishsu/d29c86fdf8b22cd41403187c61c7934a to your computer and use it in GitHub Desktop.
Qt GUI to show difference between two directories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import glob | |
import sys | |
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QListWidget, QHBoxLayout, QFileDialog | |
def compare_folders(dir1, dir2): | |
files_in_dir1 = [ | |
f.replace('._', '').replace(dir1 + '/', '') | |
for f in glob.glob(dir1 + '/**/*', recursive=True) | |
] | |
files_in_dir2 = [ | |
f.replace('._', '').replace(dir2 + '/', '') | |
for f in glob.glob(dir2 + '/**/*', recursive=True) | |
] | |
only_in_dir1 = sorted([os.path.join(dir1, f) for f in files_in_dir1 if f not in files_in_dir2]) | |
only_in_dir2 = sorted([os.path.join(dir2, f) for f in files_in_dir2 if f not in files_in_dir1]) | |
return {'only_in_dir1': only_in_dir1, 'only_in_dir2': only_in_dir2, 'diff_files': []} | |
class FolderCompareApp(QWidget): | |
def __init__(self): | |
super().__init__() | |
self.initUI() | |
def initUI(self): | |
# Layouts | |
self.layout = QVBoxLayout() | |
self.button_layout = QHBoxLayout() | |
# Buttons for selecting folders | |
self.btn_dir1 = QPushButton('選擇第一個資料夾') | |
self.btn_dir2 = QPushButton('選擇第二個資料夾') | |
self.btn_compare = QPushButton('比對資料夾') | |
self.btn_dir1.clicked.connect(self.select_directory_1) | |
self.btn_dir2.clicked.connect(self.select_directory_2) | |
self.btn_compare.clicked.connect(self.compare_directories) | |
# List widgets for showing results | |
self.list_only_in_dir1 = QListWidget() | |
self.list_only_in_dir2 = QListWidget() | |
self.list_diff_files = QListWidget() | |
# Adding buttons to layout | |
self.button_layout.addWidget(self.btn_dir1) | |
self.button_layout.addWidget(self.btn_dir2) | |
self.button_layout.addWidget(self.btn_compare) | |
# Adding everything to the main layout | |
self.layout.addLayout(self.button_layout) | |
self.layout.addWidget(self.list_only_in_dir1) | |
self.layout.addWidget(self.list_only_in_dir2) | |
self.layout.addWidget(self.list_diff_files) | |
self.setLayout(self.layout) | |
self.setWindowTitle('資料夾比對工具') | |
def select_directory_1(self): | |
self.dir1 = QFileDialog.getExistingDirectory(self, "選擇第一個資料夾") | |
if self.dir1: | |
self.btn_dir1.setText(self.dir1) | |
def select_directory_2(self): | |
self.dir2 = QFileDialog.getExistingDirectory(self, "選擇第二個資料夾") | |
if self.dir2: | |
self.btn_dir2.setText(self.dir2) | |
def compare_directories(self): | |
if hasattr(self, 'dir1') and hasattr(self, 'dir2'): | |
# Call your compare function here | |
results = self.compare_folders(self.dir1, self.dir2) # Assume this function returns a dictionary with comparison results | |
self.list_only_in_dir1.addItems(results['only_in_dir1']) | |
self.list_only_in_dir2.addItems(results['only_in_dir2']) | |
self.list_diff_files.addItems(results['diff_files']) | |
def compare_folders(self, dir1, dir2): | |
# Your folder comparison logic goes here | |
# Dummy return | |
return compare_folders(dir1, dir2) | |
if __name__ == '__main__': | |
app = QApplication(sys.argv) | |
ex = FolderCompareApp() | |
ex.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment