python制作一个批量更新文件名称的工具

 

 新书上架~👇全国包邮奥~

python实用小工具开发教程icon-default.png?t=N7T8http://pythontoolsteach.com/3

 欢迎关注我👆,收藏下次不迷路┗|`O′|┛ 嗷~~

目录

一.前言

二.实现

三.使用效果


一.前言

随着数字化时代的到来,文件管理和处理变得越来越重要。在日常工作和学习中,我们经常需要处理大量的文件,而文件名作为文件的重要标识,其规范性和一致性对于文件管理和检索至关重要。然而,手动修改大量文件名既繁琐又容易出错。因此,开发一种能够批量修改文件名的工具显得尤为重要。Python作为一种功能强大的编程语言,因其简洁易懂的语法和丰富的库函数,成为实现这一功能的理想选择。

二.实现

首先,先请大模型帮我生成代码:

生成的代码内容如下:

import tkinter as tk  
from tkinter import filedialog  
  
def select_input_directory():  
    directory = filedialog.askdirectory()  
    if directory:  
        input_list.insert(tk.END, directory)  
  
def select_output_directory():  
    directory = filedialog.askdirectory()  
    if directory:  
        output_list.insert(tk.END, directory)  
  
# 创建主窗口  
root = tk.Tk()  
root.title("目录选择器")  
  
# 创建“输入目录”listbox和按钮  
input_frame = tk.Frame(root)  
input_frame.pack(pady=10)  
input_label = tk.Label(input_frame, text="输入目录:")  
input_label.pack(side=tk.LEFT)  
input_list = tk.Listbox(input_frame)  
input_list.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)  
input_button = tk.Button(input_frame, text="选择目录", command=select_input_directory)  
input_button.pack(side=tk.LEFT, padx=5)  
  
# 创建“输出目录”listbox和按钮  
output_frame = tk.Frame(root)  
output_frame.pack(pady=10)  
output_label = tk.Label(output_frame, text="输出目录:")  
output_label.pack(side=tk.LEFT)  
output_list = tk.Listbox(output_frame)  
output_list.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)  
output_button = tk.Button(output_frame, text="选择目录", command=select_output_directory)  
output_button.pack(side=tk.LEFT, padx=5)  
  
# 运行主循环  
root.mainloop()

运行后得到如下窗口:

 再加个转换按钮用于调用后端转换文件名的功能,跟大模型对话:

得到如下代码:

import tkinter as tk  
from tkinter import filedialog  
  
def select_input_directory():  
    directory = filedialog.askdirectory()  
    if directory:  
        input_list.insert(tk.END, directory)  
  
def select_output_directory():  
    directory = filedialog.askdirectory()  
    if directory:  
        output_list.insert(tk.END, directory)  
  
def convert():  
    # 这里可以添加转换逻辑  
    input_dir = input_list.get(tk.ACTIVE)  
    output_dir = output_list.get(tk.ACTIVE)  
    print(f"转换逻辑开始执行,输入目录:{input_dir},输出目录:{output_dir}")  
    # 示例逻辑,实际应用中需要根据需求实现具体的转换功能  
  
# 创建主窗口  
root = tk.Tk()  
root.title("目录选择器与转换器")  
  
# 创建“输入目录”listbox和按钮  
input_frame = tk.Frame(root)  
input_frame.pack(pady=10)  
input_label = tk.Label(input_frame, text="输入目录:")  
input_label.pack(side=tk.LEFT)  
input_list = tk.Listbox(input_frame)  
input_list.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)  
input_button = tk.Button(input_frame, text="选择目录", command=select_input_directory)  
input_button.pack(side=tk.LEFT, padx=5)  
  
# 创建“输出目录”listbox和按钮  
output_frame = tk.Frame(root)  
output_frame.pack(pady=10)  
output_label = tk.Label(output_frame, text="输出目录:")  
output_label.pack(side=tk.LEFT)  
output_list = tk.Listbox(output_frame)  
output_list.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)  
output_button = tk.Button(output_frame, text="选择目录", command=select_output_directory)  
output_button.pack(side=tk.LEFT, padx=5)  
  
# 创建“转换”按钮  
convert_frame = tk.Frame(root)  
convert_frame.pack(pady=10)  
convert_button = tk.Button(convert_frame, text="转换", command=convert)  
convert_button.pack()  
  
# 运行主循环  
root.mainloop()

运行效果如下:

 为了实现核心功能,向大模型请教:

得到功能代码:

import os  
import shutil  
  
# 设置目录A和目录B的路径  
dir_a = '/path/to/directoryA'  
dir_b = '/path/to/directoryB'  
  
# 确保目录B存在,如果不存在则创建  
if not os.path.exists(dir_b):  
    os.makedirs(dir_b)  
  
# 遍历目录A中的文件  
for filename in os.listdir(dir_a):  
    # 构建源文件和目标文件的完整路径  
    source_file = os.path.join(dir_a, filename)  
    target_file = os.path.join(dir_b, 'conversed-' + filename)  
      
    # 确保处理的是文件而不是目录  
    if os.path.isfile(source_file):  
        # 复制并重命名文件到目录B  
        shutil.copy2(source_file, target_file)  
        print(f"Copied and renamed '{filename}' to '{dir_b}/conversed-{filename}'")

 最后实现功能完整代码:

import tkinter as tk  
from tkinter import filedialog  
import os  
import shutil  
def select_input_directory():  
    directory = filedialog.askdirectory()  
    if directory:  
        input_list.insert(tk.END, directory)  
  
def select_output_directory():  
    directory = filedialog.askdirectory()  
    if directory:  
        output_list.insert(tk.END, directory)  
  
def convert():  
    # 这里可以添加转换逻辑  
    input_dir = input_list.get(tk.ACTIVE)  
    output_dir = output_list.get(tk.ACTIVE)  
    print(f"转换逻辑开始执行,输入目录:{input_dir},输出目录:{output_dir}")  
    # 将输入目录的内容重命名为指定格式。
    # 遍历目录A中的文件  
    for filename in os.listdir(input_dir):  
        # 构建源文件和目标文件的完整路径  
        source_file = os.path.join(input_dir, filename)  
        target_file = os.path.join(output_dir, 'conversed-' + filename)  
      
        # 确保处理的是文件而不是目录  
        if os.path.isfile(source_file):  
            # 复制并重命名文件到目录B  
            shutil.copy2(source_file, target_file)  
            print(f"Copied and renamed '{filename}' to '{output_dir}/conversed-{filename}'")

  
# 创建主窗口  
root = tk.Tk()  
root.title("目录选择器与转换器")  
  
# 创建“输入目录”listbox和按钮  
input_frame = tk.Frame(root)  
input_frame.pack(pady=10)  
input_label = tk.Label(input_frame, text="输入目录:")  
input_label.pack(side=tk.LEFT)  
input_list = tk.Listbox(input_frame)  
input_list.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)  
input_button = tk.Button(input_frame, text="选择目录", command=select_input_directory)  
input_button.pack(side=tk.LEFT, padx=5)  
  
# 创建“输出目录”listbox和按钮  
output_frame = tk.Frame(root)  
output_frame.pack(pady=10)  
output_label = tk.Label(output_frame, text="输出目录:")  
output_label.pack(side=tk.LEFT)  
output_list = tk.Listbox(output_frame)  
output_list.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)  
output_button = tk.Button(output_frame, text="选择目录", command=select_output_directory)  
output_button.pack(side=tk.LEFT, padx=5)  
  
# 创建“转换”按钮  
convert_frame = tk.Frame(root)  
convert_frame.pack(pady=10)  
convert_button = tk.Button(convert_frame, text="转换", command=convert)  
convert_button.pack()  
  
# 运行主循环  
root.mainloop()

三.使用效果

 非常感谢您花时间阅读我的博客,希望这些分享能为您带来启发和帮助。期待您的反馈与交流,让我们共同成长,再次感谢!

👇热门内容👇 

python使用案例与应用_安城安的博客-CSDN博客

软硬件教学_安城安的博客-CSDN博客

Orbslam3&Vinsfusion_安城安的博客-CSDN博客

网络安全_安城安的博客-CSDN博客

教程_安城安的博客-CSDN博客

python办公自动化_安城安的博客-CSDN博客

👇个人网站👇

安城安的云世界

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值