Python 檔案批量重新命名

2021-09-25 06:43:27 字數 518 閱讀 6499

今天正好需要給團隊做乙個批量重新命名的工作,就想用python解決一下:

import os

path =

'e:/02組素材收集/摳圖'

l = os.listdir(path)

os.chdir(path)

# 這一行重中之重,之前沒加一直報錯,後來加上這行才執行成功

forfile

in l:

try:

iffile

.split(

'-')[0

]=='1':

# 我們的重新命名規則很簡單,就是把檔案中「1-」去掉,這個地方不同規則需要重新寫

t =file

.replace(

'1-',''

) os.rename(

file

, t)

except filenotfounderror as e:

print

(e)

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...

python檔案批量重新命名

如下 coding gbk import os class filerename def init self,path self.path path self.order self.files os.listdir path def list self return self.files def p...