python小程序之文件整理

设计思路

    逻辑

        输入/出源文件地址

        判断路径是否是空

        输入的是汉字还是英文(大小写)

        输入的文件夹目录是否为空

        输出的路径文件是否存在

        用到的主要模块os,re

        主要用到的函数os.walk(path)

前台设计

python小程序之文件整理_第1张图片

 

运行效果图

python小程序之文件整理_第2张图片

代码展示:

 

 

import sys
import os
import shutil
import re
import tkinter
import time
import datetime

def shmsg(text):
    root=tkinter.Tk()
    w=tkinter.Frame(root)
    w.pack(expand=1,fill=tkinter.BOTH)
    lab = tkinter.Label(w, text=text ,bg='red')
    lab.pack(fill=tkinter.BOTH)
    root.mainloop()
def frame(root,side):
    w=tkinter.Frame(root)
    w.pack(side=side,expand=1,fill=tkinter.BOTH)
    return w
def label(root,side,text,bg=None,):
    lab = tkinter.Label(root, text=text ,bg=bg,width = 9)
    lab.pack(side=side,fill=tkinter.BOTH)
    return lab
def entry(root,vals,side):
    path=tkinter.Entry(root , width=30,textvariable=vals)
    path.pack(side=side,expand=1,fill=tkinter.BOTH)
    return path
def button(root,text,side,command=None):
    butn=tkinter.Button(root,text=text,command=command)
    butn.pack(side=side,expand=1,fill=tkinter.BOTH)
    return butn
def text(root,side):
    text = tkinter.Text(root)
    text.pack(side=side,expand=1,fill=tkinter.BOTH)
    return text
def load(text,fi):
    # text.delete(0.0,tkinter.END)
    text.insert(tkinter.INSERT,fi+'\n')
 
def seEeg(str):
    # 将文件明的英文查出
    rule=re.compile(r'[^a-zA-z]')
    result = rule.sub('',str)
    return result
 
def is_english(uchar):
    """判断一个unicode是否是英文
    :param uchar:
    """
    if (uchar >=  '\u0041'and uchar<= '\u005a') or (uchar >=  '\u0061'and uchar<= '\u007a'):
        return True
    else:
        return False
def movefiles(strs,path,tarPath,text):
    mf=''
    msg=''
    if (len(strs)==0)|(len(path)==0)|(len(tarPath)==0):
        msg='亲!输入项不能为空哦!'
    else:
        for root,dirs,files in os.walk(path):
            if(len(files)==0):
                 msg='亲!目标不存在文件呦!'
            else:
                for file in files:
                    ext=file.split('.')
                    ext=ext[-1]
                    if ext in ['pdf','txt','docx','xlsx','chm','doc','xls']:
                        if is_english(strs):
                            name=seEeg(file)
                            m = re.search(strs, name, re.IGNORECASE)
                        else:
                            m = re.search(strs, file)
                        if m:
                            size = os.path.getsize(root+os.sep+file)
                            size=size/1000000
                            if size > 10:
                                msg=file+'文件大于10M,请耐心等待O(∩_∩)O~'
                                # load(text,msg)
                                continue
                            if os.path.exists(tarPath+os.sep+file):
                                msg=file+'已经存在于'+tarPath+'下面,请确认目标文件夹是否为空'
                                # load(text,msg)
                                break
                            print('-------------------------------------')
                            shutil.move(root+os.sep+file,tarPath)
                            mf=file
                            load(text,msg)
                            load(text,mf)
                            load(text,'-----------------------------')
    load(text,msg)
    load(text,'程序已经运行结束!请查看')
    load(text,datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
    load(text,'-----------------------------------------------------------------')
def selefiles(strs,path,tarPath,text):
    msg=''
    if (len(strs)==0)|(len(path)==0)|(len(tarPath)==0):
        msg='亲!输入项不能为空哦!'
    else:
        for root,dirs,files in os.walk(path):
            if(len(files)==0):
                msg='亲!目标不存在文件呦!'
            else:
                for file in files:
                    ext=file.split('.')
                    ext=ext[-1]
                    if ext in ['pdf','txt','docx','xlsx','chm','doc','xls']:
                        if is_english(strs):
                            name=seEeg(file)
                            m = re.search(strs, name, re.IGNORECASE)
                        else:
                            m = re.search(strs, file)
                        if m:
                            load(text,file)
    load(text,msg)
    load(text,'程序已经运行结束!请查看')
    load(text,datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
    load(text,'-----------------------------------------------------------------')

if __name__=='__main__':
    root=tkinter.Tk()
    root.title("文件整理工具")
    toframe=frame(root,'top')
    enframe=frame(root,'top')
    en3frame=frame(root,'top')
    en4frame=frame(root,'top')
    en5frame=frame(root,'top')
    en6frame=frame(root,'top')
    covals= tkinter.StringVar()
    tavals= tkinter.StringVar()
    kevals= tkinter.StringVar()
    lab1=label(toframe,'left','查找目录')
    lab2=label(enframe,'left','目标目录')
    lab3=label(en3frame,'left','查找关键字','green')
    coentry=entry(toframe,covals,'right')
    taentry=entry(enframe,tavals,'right')
    keentry=entry(en3frame,kevals,'left')
    text=text(en5frame,'top')
    cobutton=button(en4frame,"查找",'left',command=lambda path=covals,tarPath=tavals,strs=kevals,text=text:selefiles(strs.get(),path.get(),tarPath.get(),text))
    tabutton=button(en4frame,"开始移动",'right',command=lambda path=covals,tarPath=tavals,strs=kevals,text=text:movefiles(strs.get(),path.get(),tarPath.get(),text))
    root.mainloop()

 

你可能感兴趣的:(python小程序之文件整理)