用python對檔案批量重新命名

2021-09-24 10:04:32 字數 1084 閱讀 1850

執行環境:python 3.6.0

問題:檔案多,需要重新命名的檔案太多,學了變成語言還要手動更改嗎?完全不用,能用機器自己完成的絕不靠手動完成。

程式執行前:

程式執行後:

# function: 檔案重新命名

# author: elvisct

# time: 2023年6月12日

import os

path = "c:/users/administrator/desktop/video"

files = os.listdir(path=path) # 獲取位址裡有的檔案的名稱,得到乙個列表

for file in files:

# old_file = "{}/{}".format(path, file)

# new_file = "{}/{}".format(path, file)

old_file = os.path.join(path, file) # 舊的檔案位址,位址加檔名

new_file = os.path.join(path, file + ".mp4") # 新的檔案位址,位址加檔名,增加 mp4 字尾名

# new_file = os.path.join(path, file).replace(".mp4", '') # 去掉 mp4 字尾名

if os.path.isfile(old_file):

os.renames(old_file, new_file) # 檔案重新命名

print(new_file)

ps:以上為批量新增字尾名的示例,也可通過相似手法更改為不同的檔案

Python 檔案批量重新命名

今天正好需要給團隊做乙個批量重新命名的工作,就想用python解決一下 import os path e 02組素材收集 摳圖 l os.listdir path os.chdir path 這一行重中之重,之前沒加一直報錯,後來加上這行才執行成功 forfile in l try iffile s...

Python檔案批量重新命名

把某一資料夾下的所有檔案 如 名稱統一為序號的排列,並可以更改檔案的字尾 import os def rename i 0 path r home val3 filelist os.listdir path 該資料夾下所有的檔案 包括資料夾 for files in filelist 遍歷所有檔案 ...

Python檔案批量重新命名

需求 python 檔案批量重新命名1.在當前目錄新建目錄img,裡面包含100個檔案,100個檔名各不相同 x4g5.png 2.將當前img目錄所有以.png結尾的字尾名改為.jpg.import random import string import os from os.path impor...