【python】批量修改文件名

from tkinter import *
import tkinter
import tkinter.filedialog
import  os
import tkinter.messagebox


#获取扩展名
def GetFileExtension(path):
 return os.path.splitext(path)[1]

def choosesourcepath():
    selectFileName = tkinter.filedialog.askdirectory(title='选择文件夹')  # 选择文件
    sourcepathtext.set(selectFileName)

def choosedescpath():
    selectFileName = tkinter.filedialog.askdirectory(title='选择文件夹')  # 选择文件
    descpathtext.set(selectFileName)


def hint(filename,newname):
    result=tkinter.messagebox.askokcancel('提示', '要执行此操作吗 ?\n  '+"示例:" + filename + " ==> " +newname)
    return result
def hint_finish():
    tkinter.messagebox.askokcancel('提示', '转换完成!')

def modifyname():

    serial=beginserial.get()
    files = os.listdir(sourcepathtext.get())  # 列出当前目录下所有的文件
    files.sort()
    continueflag = False
    for filename in files:
        if filename == runname:
            continue
        srcfullname = sourcepathtext.get() + "/" + filename
        allflagnum=srcfullname.count(splitflag.get())
        orifilename= filename.split(str(splitflag.get()))
        splitnum=splitpostion.get()
        if allflagnum < splitnum:
            splitnum = allflagnum
        orifilename=str(splitflag.get()).join(orifilename[splitnum::])
        print(orifilename)

        if os.path.isfile(srcfullname):
            newname="T" + str(serial).zfill(3)+"_"+ orifilename
            if continueflag == False:
                result = hint(filename,newname)
                if result == False:
                    return
                else:
                     continueflag=True
            os.rename(sourcepathtext.get()+"/"+filename,sourcepathtext.get()+"/"+newname)
            serial=int(serial)+1
    hint_finish()

runname = os.path.basename(sys.argv[0])
window = tkinter.Tk()
window.title('批量序号命名工具 作者:xiaym 日期:2019-09-10')
window.geometry('600x250')

xplace=10
yplace=10
#路径文本框
sourcepathtext = tkinter.StringVar()
sourcepathtext.set("./")
e_entry = tkinter.Entry(window, width=48, textvariable=sourcepathtext).place(x=xplace, y=yplace)
beginserial = tkinter.StringVar()
beginserial.set("10")
e_entry = tkinter.Entry(window, width=48, textvariable=beginserial).place(x=xplace, y=yplace*5)


splitflag = tkinter.StringVar()
splitflag.set("_")
e_entry = tkinter.Entry(window, width=20, textvariable=splitflag).place(x=xplace, y=yplace*10)

tkinter.Label(window, text='表名分割符').place(x=xplace*20, y=yplace*10)

splitpostion = tkinter.IntVar()
splitpostion.set("1")
e_entry = tkinter.Entry(window, width=20, textvariable=splitpostion).place(x=xplace , y=yplace*15)
tkinter.Label(window, text='第几个分割符后做为文件名').place(x=xplace*20, y=yplace*15)

files = os.listdir(sourcepathtext.get())#列出当前目录下所有的文件
submit_button = tkinter.Button(window, text ="选择文件夹", command = choosesourcepath).place(x=xplace*40, y=yplace)
tkinter.Label(window, text='文件的序号').place(x=xplace*40, y=yplace*5)
submit_button = tkinter.Button(window, text ="   转换  ", command = modifyname).place(x=xplace*20, y=yplace*20)

window.mainloop()# 进入消息循环


在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值