序
偶爾遇見要對檔案名批量修改,部分檔案名替換時候場景,
思路是遍歷檔案名,然后對每個檔案名重命名替換,打包成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檔案夾里
執行效果



轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516313.html
標籤:Python
上一篇:方法的多種呼叫方式

