日常便利-使用Python制作文件批量处理的exe,思路是遍历文件名,然


偶尔遇见要对文件名批量修改,部分文件名替换时候场景。
思路是遍历文件名,然后对每个文件名重命名替换,打包成exe

Python代码


import os

try:

    file_dir = input("请输入文件路径:")
    file_dir = file_dir.replace("\\", "/")

    FindWord = input("输入要被替换的文字:")
    NewWord = input("输入替换的文字:")
    for root, dirs, files in os.walk(file_dir, topdown=False):
        # print(root)  # 根路径
        # print(files)  # 非目录文件
        # print(dirs)  # 目录文件

        # 遍历得到非目录文件名
        for file in files:
            # 生成新的文件名
            newFile = file.replace(FindWord, NewWord)
            print(newFile)
            # 重命名
            os.rename(root + "/" + file, root + "/" + newFile)
    # 防止exe闪退
    os.system('pause')
    
except Exception as e:
    print(e)
    print("请输入正确的路径")
    os.system('pause')


打包成为exe

pyinstaller -F -i 'ic.ico' updataFileName.py
其中,-i 是指定图标,-F是打包成一个独立可运行的exe,最后XXX.py是你要打包的运行的文件。最终会出现在dist文件夹

执行效果



评论关闭