Python获取指定类型文件

Python获取指定类型文件

PS: 也是从网上各个帖子中学习的Python,因此代码的格式以及内容有粘贴网上其他大神的代码,如有侵权请告知删除

示例图片
在这里插入图片描述

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
from tkinter import *
import tkinter.filedialog
import shutil

root = Tk()
root.title('类型文件筛选')
root.geometry('390x300')
FolderName1 = ""
FolderName2 = ""

def Button1Anwser():
    global FolderName1
    Path = Label(root, text='')
    FolderName1 = tkinter.filedialog.askdirectory()
    Path.config(text= FolderName1)
    Path.grid(row=1, column=0, sticky=W)

def Button2Anwser():
    global FolderName2
    Path = Label(root, text='')
    FolderName2 = tkinter.filedialog.askdirectory()
    Path.config(text= FolderName2)
    Path.grid(row=3, column=0, sticky=W)


def Start():
    FileType = Type.get()
    Text = Label(root, text='')
    Text.config(text='正在筛选')
    Text.grid(row=6, column=1)
    ChildType = FileType.split(",")
    #对源文件夹中的文件进行遍历
    for FolderName, SubFolders, FileNames in os.walk(FolderName1):
        for FileName in FileNames:
            # 该变量由于重命名同名文件,即在原有名字后面加上1,2,3,4……
            n = 1
            # 该变量由于重命名同名但大小写不同的文件,即在原有名字后面加上1,2,3,4……
            j = 1
            #sourceFile为将要复制的文件
            sourceFile = os.path.join(FolderName + '\\' + FileName)
            for i in range(0,len(ChildType)):
                # 当文件名中存在输入的后缀名
                if re.compile(ChildType[i]).search(FileName) != None:
                    #如果目标文件夹为空
                    if len(os.listdir(FolderName2)) > 0:
                        #遍历目标文件夹
                        for DesFolderName, DesSubFolders, DesFileNames in os.walk(FolderName2):
                            for DesFileName in DesFileNames:
                                # 名字相同
                                if FileName.upper() == DesFileName.upper():
                                    if FileName != DesFileName:
                                        print(FileName)
                                        print(DesFileName)
                                        # 重命名同名文件(大小写不同)
                                        DesFile = os.path.join(
                                            FolderName2 + '\\' + FileName.split('.')[0] + str(j) + "." +
                                            FileName.split('.')[1])
                                        j = j+1
                                        # 对同名文件重新命名后进行复制粘贴
                                        print(sourceFile)
                                        print(DesFile)
                                        shutil.copy(sourceFile, DesFile)
                                        #为了避免下方重复复制粘贴文件
                                        CopyFinish = 1
                                    # 大小相同
                                    if os.path.getsize(os.path.join(FolderName + '\\' + FileName)) == os.path.getsize(os.path.join(DesFolderName + '\\' + DesFileName)):
                                        # 创建时间相同
                                        if os.path.getctime(os.path.join(FolderName + '\\' + FileName)) == os.path.getctime(os.path.join(DesFolderName + '\\' + DesFileName)):
                                            # 计算源文件在在目标文件夹中有多少个同名文件,方便计算名字后面加的数值
                                            n = n+1
                        if n > 1:
                            #重命名同名文件
                            DesFile = os.path.join(
                                FolderName2 + '\\' + FileName.split('.')[0] + str(n) + "." + FileName.split('.')[1])
                            #对同名文件重新命名后进行复制粘贴
                            shutil.copy(sourceFile, DesFile)
                        # 如果没有同名,进行复制粘贴
                        else:
                            if CopyFinish == 0:
                                shutil.copy(sourceFile, FolderName2)
                            else:
                                CopyFinish = 0
                    #在目标文件夹为空的情况,直接将源文件进行复制粘贴
                    else:
                        shutil.copy(sourceFile, FolderName2)
                    break
    Text = Label(root, text='')
    Text.config(text='筛选完成')
    Text.grid(row=6, column=1)


# -------------------------------------界面---------------------------------

SelectFolder1 = Button(root, text="源文件夹", command=Button1Anwser, width=10, height=1, bg="DarkGray", fg="white")
SelectFolder1.grid(row=0, column=0, sticky=W)

SelectFolder2 = Button(root, text="目标文件夹", command=Button2Anwser,width=10, height=1, bg="DarkGray", fg="white")
SelectFolder2.grid(row=2, column=0, sticky=W)

Text1 = Label(root, text='想要筛选出什么类型的文件(后缀名)')
Text1.grid(row=4, sticky=W)
Type = Entry(root)
Type.grid(row=5, column=0, sticky=W)

StartAction = Button(root, text="开始筛选", command=Start,width=10, height=1, bg="DarkGray", fg="white")
StartAction.grid(row=6, sticky=W)

Text2 = Label(root, text='PS:筛选多类型时,类型之间用逗号隔开,如png,jpg,bmp')
Text2.grid(row=7, sticky=W)
root.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值