python批量命名教程_【Python-GUI】批量文件名修改程序

正文之前前阵子,大概是因为闲得慌,也是恼火那些给我交电子表格的人,怎么如此不注重格式 ,所以瞎鸡儿用python写了个批量修改文件名的程序。。大家可以稍微看看 正文import osfrom tkinter import *from tkinter.filedialog import askdirectorydef renameFile(filepath,formatofFile,houzhui,...
摘要由CSDN通过智能技术生成

正文之前

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

a518e73e2dcf3a164fb7130fca9bcff8.png

正文

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()

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

4a739b7192bcb351c73a2d66a58d315f.png

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

d4989679489b5472febe2fb66a4401db.png

这就是效果图了~

a721963d2bc57e5f60c86ad344d5356a.png

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

82c79b31cb169d0576808c4ff11d135a.png

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

正文之后

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值