目录文件比较工具

news/2025/3/14 17:13:28/

最近老比较两个目录的文件列表是否相同,需要将源目录中多出的文件自动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 filedialogdef 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_listdef 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_filesclass MyGui:def __init__(self, gui):self._gui = guiself.listbox = Noneself.listbox1 = Noneself.src_folder = []self.target_folder = []self.check_box_var1 = Nonedef 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 = 0for file in missing_file_list:file_path = os.path.join(file[2], file[1])shutil.copy(file_path, self.target_folder[0])index = index + 1print("{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(root, text="选择源文件夹", width=12, command=self.select_folder)button.grid(row=0, column=0)# button.pack()button1 = tk.Button(root, text="选择目标文件夹", width=12, command=self.select_target_folder)button1.grid(row=1, column=0)# button1.pack()self.listbox = tk.Listbox(root, width=66, height=8)  # 创建两个列表组件self.listbox.grid(row=0, column=1)# listbox.pack()self.listbox1 = tk.Listbox(root, width=66, height=8)self.listbox1.grid(row=1, column=1)# listbox1.pack()button2 = tk.Button(root, text="filelist compare", width=12, command=self.filelist_compare)button2.grid(row=2, column=0)checkbox = tk.Checkbutton(root, 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(root, 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()

http://www.ppmy.cn/news/1138173.html

相关文章

js中的call和apply方法

this总是指向调用时的对象,但是apply()和call()可以修改this指向。 call()方法 语法:obj.call(thisObj, arg1, arg2…) call的第一个参数就是要修改的this所在的对象,其余参数是是作为参数传入到调用的方法上。 注意: 如果没…

NOIP-2023模拟题

全国青少年信息学奥林匹克竞赛 NOIP2023模拟 时间:8:00-12:30 题目名称异或游戏连通块公交路线题目类型传统型传统型传统型传统型目录 xor \text{xor} xor game \text{game} game connect \text{connect} connect route \text{route} route可执行文件名 xor \text{x…

DBC配置SecOC属性

关联文章:Autosar基础——车载信息安全SecOC 属性定义的规范详细介绍请参考 ①dbc属性定义 ②Vector DBC属性定义规则 文章目录 一、SecOC简介二、DBC文件中的SecOC属性三、配置SecOC属性设置SecOC的属性设置同步报文的属性设置同步请求报文的属性一、SecOC简介 在车载网络中…

Python(八十八)函数的参数传递

❤️ 专栏简介:本专栏记录了我个人从零开始学习Python编程的过程。在这个专栏中,我将分享我在学习Python的过程中的学习笔记、学习路线以及各个知识点。 ☀️ 专栏适用人群 :本专栏适用于希望学习Python编程的初学者和有一定编程基础的人。无…

速学数据结构 | 手把手教你会单链表的构建方式

🎬 鸽芷咕:个人主页 🔥 个人专栏: 《初阶数据结构》《C语言进阶篇》 ⛺️生活的理想,就是为了理想的生活! 文章目录 📋 前言1. 什么是链表1.1 链表的物理结构1.2 链表的种类 2. 链表的实现一. SList.h 单链表的声明3.…

手把手教你从零开始腾讯云服务器部署(连接建站教程)

使用腾讯云服务器搭建网站全流程,包括轻量应用服务器和云服务器CVM建站教程,轻量可以使用应用镜像一键建站,云服务器CVM可以通过安装宝塔面板的方式来搭建网站,腾讯云服务器网txyfwq.com分享使用腾讯云服务器建站教程,…

二叉树经典OJ题

二叉树的层序遍历 1.题目2.图文分析3.代码演示 1.题目 2.图文分析 3.代码演示

力扣 -- 873. 最长的斐波那契子序列的长度

解题步骤&#xff1a; 参考代码&#xff1a; class Solution { public:int lenLongestFibSubseq(vector<int>& nums) {int nnums.size();unordered_map<int,int> hash;for(int i0;i<n;i){hash[nums[i]]i;}int ret2;vector<vector<int>> dp(n,v…