python 批量修改文件名

很多朋友遇到批量修改文件名的问题,网上各种搜,操作麻烦不说还有些需要付费。这里不多废话,直接上代码。

一、支持库

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

二、 定义函数

def Rename():  # 处理粘贴上来的数据
    global var1,var2
    i = 0
    path0 = entry.get('0.0','end')  #获取输入框内容
    path0 = path0.replace('
','')
    path = filedialog.askdirectory()  #加载需要改名的文件
    files = os.listdir(path)
    #判断 复选框情况
    if (var1.get() == 1 and var2.get() == 0 and var3.get() == 0):   #加到文件名前面
        for aa in files:
            # 旧名称
            old = os.path.join(path, aa)
            kzm = os.path.splitext(aa)[-1]
            picture = aa[:-4]
            # 新名称
            new = picture.replace(picture, path0 + picture + kzm)
            # 加上路径
            new = os.path.join(path, new)
            # 开始修改文件名称
            os.rename(old, new)
    elif (var1.get() == 0 and var2.get() == 1 and var3.get() == 0):  #加到文件名后面
        for aa in files:
            # 旧名称
            old = os.path.join(path, aa)
            kzm = os.path.splitext(aa)[-1]
            picture = aa[:-4]
            # 新名称
            new = picture.replace(picture, picture + path0 + kzm)
            # 加上路径
            new = os.path.join(path, new)
            # 开始修改文件名称
            os.rename(old, new)
    elif (var1.get() == 0 and var2.get() == 0 and var3.get() == 1):  #全部修改
        for aa in files:
            i = i + 1
            # 旧名称
            old = os.path.join(path, aa)
            kzm = os.path.splitext(aa)[-1]
            picture = aa[:-4]
            # 新名称
            new = picture.replace(picture, path0 + '-' + str(i) + kzm)
            # 加上路径
            new = os.path.join(path, new)
            # 开始修改文件名称
            os.rename(old, new)
    else:
        messagebox.showerror(message="只能勾选一个,不能不选!!!")
    messagebox.showinfo('小叶提醒:', '批量修改文件名已完成!!!')
    os.startfile(path)  #改好之后打开文件夹
    root.quit()

三、程序入口

if __name__ == '__main__':
    root = Tk()  # 创建一个窗体,通过这个窗口选择指定的目标文件夹,目标文件夹存放着很多PDF文件,我们要通过这个程序在这个文件夹中筛选出需要的文件
    root.title('批量修改文件名')
    root.geometry("210x230+700+300")
    var1 = tkinter.IntVar()
    var2 = tkinter.IntVar()
    var3 = tkinter.IntVar()
    ck1 = tkinter.Checkbutton(root, text="加到文件名前面",font=('黑体', 15), height=1, variable=var1)
    ck1.place(x=5, y=40)
    ck2 = tkinter.Checkbutton(root, text="加到文件名后面",font=('黑体', 15), height=1, variable=var2)
    ck2.place(x=5, y=70)
    ck3 = tkinter.Checkbutton(root, text="全部修改",font=('黑体', 15), height=1, variable=var3)
    ck3.place(x=5, y=100)
    entry = Text(root, font=('宋体', 15), bg='#E6E8FA')  # 设置输入框
    entry.place(x=5, y=5, width=200, height=30)  # 显示组件
    #entry3.insert(INSERT, "输入文件名需要增加的字段")  # INSERT 光标处插入 END 末尾处插入
    btn_OK = tkinter.Button(root, text='*更*改*', font=('宋体', 18, 'bold'), height=2, bg='#1E90FF',
                            fg='#FFFFFF', command=lambda: Rename())
    btn_OK.place(x=45, y=140)
    root.mainloop()

四、运行,微信下载:https://share.weiyun.com/Jld8hls1

展开阅读全文

页面更新:2024-03-27

标签:文件名   批量   黑体   文件名称   文件夹   路径   名称   目标   文件   程序

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top