Python:批量改文件名(Windows版)

点触鼠标,轻松实现文件序列化更名。

 

代码涉及文件夹及文件操作、正则表达式、多线程和tkinter等知识。

用法:

1. 点击“Click me”按钮

2. 在弹出窗口中选择一个文件夹

3. 点击“Choose”执行更名或“取消”放弃更名

4. 撤销所有序列化更名,请点击“Undo”

5. 点击“Check”按钮查看操作结果

6. 下方会动态显示处理结果

所有隐藏文件或子文件夹将被略过。

# -*-coding:utf-8-*-
"""This is a free tool which easily adds sequence numbers to names of files
in a selected folder, just by clicking your mouse few times.

Here is the usage:
1. Press the 'Click me' button.
2. Select a folder in the pop-up window.
3. Click 'Choose' to execute the operation or 'Cancel' to give it up.
4. 'Undo' button is used to remove sequence numbers.
5. Press 'Check' to make sure the operation has been completed successfully.

Note that operations on hidden files or sub-folders are neglected.
"""
import os
import sys
import re
import win32file
import win32con
import threading
import tkinter as tk
from tkinter import filedialog


class EasyReNamer(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("EasyReNamer V3.0")
        self.n = 0
        label_info = tk.Label(self, text="Please select a folder:")
        label_info.pack()
        panel = tk.Frame()
        self.btn_rename = tk.Button(panel, text="Click Me", width=10,
                               bg='orange',
                               command=threading.Thread(target=self.rename).start)
        self.btn_rename.grid(row=0, column=0, padx=30)
        self.btn_undo = tk.Button(panel, text="Undo", width=10,
                             bg="grey",
                             command=self.undo)
        self.btn_undo.grid(row=0, column=1, padx=30)
        btn_check = tk.Button(panel, text="Check", width=10,
                              bg='blue', fg = 'white', 
                              command=self.check)
        btn_check.grid(row=0, column=2, padx=30)
        panel.pack()
        self.label_show = tk.Label(self)
        self.label_show.pack()

    def widget_position(self, width, height):
        self.width = self.winfo_screenwidth()
        self.height = self.winfo_screenheight()
        x = (self.width - width) / 2
        y = (self.height - height) / 2
        self.geometry('%dx%d+%d+%d' % (width, height, x, y))
        self.grid()

    def get_items_list(self):
        try:
            self.folder_path = filedialog.askdirectory(title='EasyReNamer')
            self.items_list = os.listdir(self.folder_path)
        except OSError:
            print("Opening failed")
            sys.exit()

    def rename(self):
        self.get_items_list()
        self.btn_rename.config(state='disabled')
        for item in self.items_list.copy():
            item_path = self.folder_path + os.sep + item
            file_flag = win32file.GetFileAttributesW(item_path)
            is_hiden = file_flag & win32con.FILE_ATTRIBUTE_HIDDEN
            if os.path.isdir(item_path) or  is_hiden:
                continue
            else:
                new_item_path = self.folder_path + os.sep + '(' + \
                                str(self.n + 1) + ')' + item
                os.rename(item_path, new_item_path)
                self.n += 1
                self.label_show.config(text="{} file(s) renamed".format(self.n))
        self.label_show.config(text="{} file(s) completed successfully".format(self.n))

    def undo(self):
        self.get_items_list()
        for item in self.items_list.copy():
            item_path = self.folder_path + os.sep + item
            file_flag = win32file.GetFileAttributesW(item_path)
            is_hiden = file_flag & win32con.FILE_ATTRIBUTE_HIDDEN
            if os.path.isdir(item_path) or  is_hiden:
                continue
            else:
                new_item_path = self.folder_path + os.sep + re.sub('\(\d*\)','',item)
                os.rename(item_path, new_item_path)
        self.label_show.config(text='Undo Finished!')

    def check(self):
        filedialog.askopenfilename()



if __name__ == "__main__":
    root = EasyReNamer()
    root.widget_position(500, 70)
    root.resizable(0, 0)
    root.mainloop()

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值