目录文件比较工具

最近老比较两个目录的文件列表是否相同,需要将源目录中多出的文件自动copy到目标文件,手动太费时费力,写了一个python脚本,与大家共享。
开始使用纯控制台,发现还是有个界面更方便一些,就用tkinter写了一个简单的界面。
下面的代码在win7+python3.8中正常运行。
操作方式:

  1. 选择源目录和目标目录
  2. 点击文件列表比较按钮,就可以把源目录多出的文件打印出来
  3. 点击文件列表比较按钮时如果checkbox被选中,就可以自动将多出的文件copy的目标文件夹中的第一个文件夹
  4. 点击clear,清除文件夹选择后,可以重新再操作。
# coding=utf-8
# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import tkinter as tk
import os
import shutil
from tkinter import filedialog


def get_file_list(root_dir):
    if not os.path.isdir(root_dir):
        return []

    files = os.listdir(root_dir)
    file_list = []
    for curr_dir, sub_dirs, files in os.walk(root_dir):
        # os.chdir(curr_dir)
        print(curr_dir)
        for file in files:
            # print(file)
            # if os.path.isfile(file):
            directory, file_name_ext = os.path.split(file)
            file_name, ext = os.path.splitext(file_name_ext)
            file_list.append((file_name, file, curr_dir))

    return file_list


def cmp_list(left, right):
    right_file_list = []
    missing_files = []
    for item in right:
        right_file_list.append(item[0])

    for file in left:
        if file[0] not in right_file_list:
            missing_files.append(file)
            print(file)
    print_hi(len(missing_files))
    return missing_files


class MyGui:
    def __init__(self, gui):
        self._gui = gui
        self.listbox = None
        self.listbox1 = None
        self.src_folder = []
        self.target_folder = []
        self.check_box_var1 = None

    def select_folder(self):
        folder_path = tk.filedialog.askdirectory()
        if folder_path not in self.src_folder:
            self.listbox.insert(0, folder_path)
            self.src_folder.append(folder_path)
            print(self.src_folder)

    def select_target_folder(self):
        folder_path = tk.filedialog.askdirectory()
        if folder_path not in self.target_folder:
            self.listbox1.insert(0, folder_path)
            self.target_folder.append(folder_path)
            print(self.target_folder)

    def filelist_compare(self):
        src_file_list = []
        target_file_list = []

        for path in self.src_folder:
            src_file_list = src_file_list + get_file_list(path)
        for path in self.target_folder:
            target_file_list = target_file_list + get_file_list(path)

        missing_file_list = cmp_list(src_file_list, target_file_list)
        if self.check_box_var1.get() == 1:
            file_count = len(missing_file_list)
            index = 0
            for file in missing_file_list:
                file_path = os.path.join(file[2], file[1])
                shutil.copy(file_path, self.target_folder[0])
                index = index + 1
                print("{0} copied to {1}, {2}/{3}".format(file_path, self.target_folder[0], index, file_count))

    def clear_folder_list(self):
        self.listbox1.delete(0, 5)
        self.listbox.delete(0, 5)
        self.src_folder = []
        self.target_folder = []

    def on_checkbox_changed(self):
        if self.check_box_var1.get() == 1:
            print("checked")
        if self.check_box_var1.get() == 0:
            print("not checked")

    def init_gui(self):
        self._gui.geometry('600x400+250+100')
        self.check_box_var1 = tk.IntVar()
        button = tk.Button(self._gui, text="选择源文件夹", width=12, command=self.select_folder)
        button.grid(row=0, column=0)
        # button.pack()
        button1 = tk.Button(self._gui, text="选择目标文件夹", width=12, command=self.select_target_folder)
        button1.grid(row=1, column=0)
        # button1.pack()
        self.listbox = tk.Listbox(self._gui, width=66, height=8)  # 创建两个列表组件
        self.listbox.grid(row=0, column=1)
        # listbox.pack()
        self.listbox1 = tk.Listbox(self._gui, width=66, height=8)
        self.listbox1.grid(row=1, column=1)
        # listbox1.pack()
        button2 = tk.Button(self._gui, text="filelist compare", width=12, command=self.filelist_compare)
        button2.grid(row=2, column=0)
        checkbox = tk.Checkbutton(self._gui, text="copy file to target",
                                       variable=self.check_box_var1,
                                       onvalue=1, offvalue=0,
                                       command=self.on_checkbox_changed)
        checkbox.grid(row=2, column=1)
        button3 = tk.Button(self._gui, text="clear", width=12, command=self.clear_folder_list)
        button3.grid(row=3, column=0)


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print("total {0} files not in target".format(name))  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    root = tk.Tk()
    mygui = MyGui(root)
    mygui.init_gui()
    # log_data_Text = tk.Text(root, width=66, height=9)  # 日志框
    # scroll = tk.Scrollbar()
    # # 放到窗口的右侧, 填充Y竖直方向
    # scroll.pack(side=tk.RIGHT, fill=tk.Y)
    # # 两个控件关联
    # scroll.config(command=log_data_Text.yview)
    #
    # log_data_Text.config(yscrollcommand=scroll.set)
    # log_data_Text.grid(row=4, column=0, columnspan=2)
    root.mainloop()



你可能感兴趣的:(windows,python,tkinter)