美文网首页工具癖程序员计算机杂谈
【Python-GUI】批量文件名修改程序

【Python-GUI】批量文件名修改程序

作者: 张照博 | 来源:发表于2018-07-30 15:49 被阅读21次

    正文之前

    前阵子,大概是因为闲得慌,也是恼火那些给我交电子表格的人,怎么如此不注重格式 ,所以瞎鸡儿用python写了个批量修改文件名的程序。。大家可以稍微看看

    正文

    import os
    from tkinter import *
    from tkinter.filedialog import askdirectory
    
    
    def renameFile(filepath,formatofFile,houzhui,outputDir):
        pathDir = os.listdir(filepath)
        for simpleFile in pathDir:
            oldFile = open(filepath+simpleFile,'rb')
            newFile = open(filepath+'backup'+simpleFile,'wb')
            contents = oldFile.readlines()
            newFile.writelines(contents)
            newFile.close()
            oldFile.close()
            zhuanye = ''
            banji = ''
            if simpleFile.find(formatofFile) != -1:
                if simpleFile.find('机械') != -1 or simpleFile.find('机制') != -1:
                    zhuanye = '机械'
                if simpleFile.find('卓') != -1:
                    zhuanye = '机卓'
                if simpleFile.find('中英')!= -1:
                    zhuanye = '机械中英'
                if simpleFile.find('工程')!= -1:
                    zhuanye = '工程'
                if  simpleFile.find('测试')!= -1 or simpleFile.find('测控')!= -1:
                    zhuanye = '测控'   
                if simpleFile.find('产设')!= -1 or simpleFile.find('产品')!= -1:
                    zhuanye = '产设'   
                banji = simpleFile[simpleFile.find('1'):simpleFile.find('1')+4]
                if houzhui.find(formatofFile) == -1:
                    newname = zhuanye+banji+houzhui+formatofFile
                else:
                    newname = zhuanye+banji+houzhui
                os.rename(filepath+simpleFile,outputDir+newname)
                print('改名'+filepath+simpleFile+' ----To---- '+outputDir+newname)
    
    r=Tk()
    pathStr = ''
    pathStrVar = StringVar()
    outpathStr = ''
    outpathStrVar = StringVar()
    formatofFile = ''
    houzhui = ''
    def pathset():
        global pathStr
        global outpathStr
        pathStr = askdirectory()
        # pathStr = pathStr[0].lower()+pathStr[2:]
        pathStr+='/'
        # print(pathStr)
        pathStrVar.set(pathStr)
    
    def outpathset():
        global pathStr
        global outpathStr
        outpathStr = askdirectory()
        # outpathStr = outpathStr[0].lower()+outpathStr[2:]
        outpathStr+='/'
        # print(outpathStr)
        outpathStrVar.set(outpathStr)
    
    def StartRename():
        global pathStr
        global outpathStr
        houzhui = entry1.get()
        formatofFile = entry.get()
        renameFile(pathStr,formatofFile,houzhui,outpathStr)
    
    r.resizable(True,True)
    screenwidth = r.winfo_screenwidth()  
    screenheight = r.winfo_screenheight()
    print(str(screenwidth)+"-->"+str(screenheight))
    Width = screenwidth // 14*5
    Height = screenheight //14*11
    size = '%dx%d+%d+%d' % (Width, Height, (screenwidth - Width)//7*3, (screenheight - Height)//14)
    print(size) 
    r.geometry(size) 
    Label(r, text = "文件夹内容名称整理器", width = 300, height = 2, fg="blue").pack(side = TOP)
    Button(r, text="点击按钮获取文件夹", command=pathset, width = 40, height = 5, bg="white", fg="black", bd = 5, font = 10).pack(side = TOP)
    Label(r, textvariable = pathStrVar, width = 300, height = 2, fg="blue").pack(side = TOP)
    Button(r, text="点击按钮选择输出文件夹\n\n请勿与输入文件夹一样~", command=outpathset, width = 40, height = 5, bg="white", fg="black", bd = 5, font = 10).pack(side = TOP)
    Label(r, textvariable = outpathStrVar, width = 300, height = 2, fg="blue").pack(side = TOP)
    
    entry = Entry(r, width = 50, bg = "white", bd = 4, fg = "blue")
    entry.pack(side = TOP)
    Label(r, text = '输入格式,如 “.xls” ,注意带点', width = 60, height = 2, fg="blue").pack(side = TOP)
    entry1 = Entry(r, width = 50, bg = "white", bd = 4, fg = "blue")
    entry1.pack(side = TOP)
    Label(r, text = '输入后缀名,如XX登记表', width = 60, height = 2, fg="blue").pack(side = TOP)
    Button(r, text="Start Batch Rename", command=StartRename, width = 40, height = 5, bg="white", fg="black", bd = 5, font = 10).pack(side = TOP)
    r.mainloop()
    

    这是改名字之前的文件布局,我随便搞的,

    这是软件的界面。对应我选择的两个文件夹以及文件类型和后缀~

    这就是效果图了~

    源文件夹内部的文件会保留,但是会加个前缀,不过这丝毫不影响再次修改。没修改好那就直接对着原文件夹再改一下就OK至于输出文件夹,自己看效果撒

    里面的很多内容是我根据当时自己的需求写的。后面就懒得弄了,优化。扩展什么的?不存在的。。有需要的小伙伴自己写吧。。我就撤了。。

    正文之后

    这个暂定为1.0版本咯~如果有同学根据这个版本修改了,可以发我一份不???

    相关文章

      网友评论

        本文标题:【Python-GUI】批量文件名修改程序

        本文链接:https://www.haomeiwen.com/subject/iwyfvftx.html