wxpython学习进阶:编写一个可以快速识别文件夹内重复文件的程序

前言:如果单纯只是要找出重复文件的话并不难,用os.walk即可,代码量不会超过30行,但是如果要再加上重命名文件、删除文件、定位文件夹,以及wxpython里的相关代码话,代码量就增长到了140行。
另外布局、美工以及wxpython里哪一个模块可以实现功能,花了我大量的时间,这一个小小的程序,花了我3个晚上的时间,自己动手编程才更加知道程序员的不易啊(当然也和我才学了半年,处于菜鸟中的菜鸟有关。。。)
PS:因为pyinstaller新版本的限定,编出来的程序不支持xp,我的测试环境是win7 64,可以使用,其中还有一些小bug,但不影响使用,具体实现功能截图如下:
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

import os
from collections import Counter
import time
import wx

def get_filePath(event):
    dlg = wx.DirDialog(window, "选择文件夹", style=wx.DD_DEFAULT_STYLE)
    if dlg.ShowModal() == wx.ID_OK:
        global  filePath
        filePath = dlg.GetPath()
    dlg.Destroy()
    refresh()

def refresh():
    window.tree.DeleteAllItems()
    repeat_file_list = get_repeat_file(filePath)
    for file in repeat_file_list:
        global repeat_fileroot_list
        repeat_fileroot_list = []
        repeat_fileroot_list = search(filePath, file)
        nan = window.tree.AppendItem(root, file)
        for i in repeat_fileroot_list:
            window.tree.AppendItem(nan, i)
    window.tree.Bind(wx.EVT_TREE_SEL_CHANGED, OnSelChanged, id=1)

def get_repeat_file(filePath):
    #首先获取文件名列表
    file_list = []
    for root, dirs, files in os.walk(filePath):
        for file in files:
            file_list.append(file)
    #再提取列表中重复的值,组成新的列表
    repeat_file_list = []
    counter = Counter(file_list)
    for item in counter:
        if counter[item] > 1:
            repeat_file_list.append(item)
    return repeat_file_list

def search(path, name=''):
    for item in os.listdir(path):
        item_path = os.path.join(path, item)
        if os.path.isdir(item_path):
            search(item_path, name)
        elif os.path.isfile(item_path):
            if name == item:
                repeat_fileroot_list.append(item_path)
    return repeat_fileroot_list

def get_FileSize(filePath):
    '''获取文件大小,以KB展示'''
    fsize = os.path.getsize(filePath)
    fsize = str(int(fsize/1024)) + 'KB'
    return fsize

def get_FileModifyTime(filePath):
    '''获取文件修改时间,以"2018-12-26 16:45:20"形式展示'''
    t = os.path.getmtime(filePath)
    timeStruct = time.localtime(t)
    return time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)

def rename_file(event):
    try:
        dlg = wx.TextEntryDialog(window, "请输入新的文件名", "重命名文件", old_filePath)
        if dlg.ShowModal() == wx.ID_OK:
            new_file = dlg.GetValue()
            os.rename(old_filePath, new_file)
            dlg = wx.MessageDialog(window, "重命名成功!", "Mission Complete!", wx.OK)
            if dlg.ShowModal() == wx.ID_OK:
                dlg.Destroy()
                refresh()
    except:
        pass

def remove_file(event):
    try:
        dlg = wx.MessageDialog(window, "请确认是否删除文件!", "删除文件", wx.OK|wx.CANCEL)
        if dlg.ShowModal() == wx.ID_OK:
            os.remove(old_filePath)
            dlg = wx.MessageDialog(window, "删除成功!", "Mission Complete!", wx.OK)
            if dlg.ShowModal() == wx.ID_OK:
                dlg.Destroy()
                refresh()
    except:
        pass

def getto_file(event):
    try:
        old_FileDir = os.path.dirname(old_filePath)
        os.system("start explorer "+old_FileDir)
    except:
        pass

def OnSelChanged(event):
    item = event.GetItem()
    global old_filePath
    old_filePath = window.tree.GetItemText(item)
    if os.path.isfile(old_filePath):
        FileSize = get_FileSize(old_filePath)
        FileModifyTime = get_FileModifyTime(old_filePath)
        temp_string = '文件路径:{}\r\n文件大小:{}\r\n修改时间:{}'.format(old_filePath, FileSize, FileModifyTime)
        window.display.SetLabel(temp_string)
    else:
        window.display.SetLabel(old_filePath)

def get_about(event):
    dlg = wx.MessageDialog(window, "This program was designed by ZhangLei, if you have some advice, please send mail to '532788667@qq.com'!", "关于我们", wx.OK)
    if dlg.ShowModal() == wx.ID_OK:
        dlg.Destroy()

hbox = wx.BoxSizer(wx.HORIZONTAL)
vbox = wx.BoxSizer(wx.VERTICAL)

app = wx.App()
window = wx.Frame(None, title="重复文件识别小程序", size=(800, 600))
panel1 = wx.Panel(window, -1)
panel2 = wx.Panel(window, -1)
# 创建菜单
menuBar = wx.MenuBar()
menu1 = wx.Menu()
menu_open = menu1.Append(wx.ID_OPEN, u"选择文件", "choose file")
menu_about = menu1.Append(wx.ID_ABOUT, "关于我们", "Information about this program") #设置菜单的内容
menuBar.Append(menu1, "系统选项")  # 菜单项目1
window.SetMenuBar(menuBar)
window.Bind(wx.EVT_MENU, get_filePath, menu_open)
window.Bind(wx.EVT_MENU, get_about, menu_about)

#左边树结构区域、右边静态文本区域的创建
window.tree = wx.TreeCtrl(panel1, 1, wx.DefaultPosition, (300, 300), wx.TR_HIDE_ROOT | wx.TR_HAS_BUTTONS)
root = window.tree.AddRoot('重复文件')
window.display = wx.StaticText(panel2, -1, '', (10, 10))

#删除文件和重命名按钮定义
rename_button = wx.Button(panel2, label = '重命名', pos = (10,100),size = (80,24))
remove_button = wx.Button(panel2, label = '删除文件', pos = (150,100),size = (80,24))
getto_button = wx.Button(panel2, label = '查看文件', pos = (290,100),size = (80,24))
rename_button.Bind(wx.EVT_BUTTON, rename_file)
remove_button.Bind(wx.EVT_BUTTON, remove_file)
getto_button.Bind(wx.EVT_BUTTON, getto_file)
vbox.Add(window.tree, 1, wx.EXPAND)
hbox.Add(panel1, 1, wx.EXPAND)
hbox.Add(panel2, 1, wx.EXPAND)
panel1.SetSizer(vbox)
window.SetSizer(hbox)
window.Center()
window.Show(True)
app.MainLoop()

很大的一个问题是,没有用类封装,这也是我接下去要努力学习的方向

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值