天天看點

Python 檔案批量命名

切圖的時候,會出來中文,修改圖檔名字很麻煩      

于是,寫了個Python 小程式

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
#傳入路徑,需要重命名的公共部分(之後,可以嘗試給檔案排序)
def changeName(path,com_name):
    files=os.listdir(path)  #傳回一個數組,list
    for index,file in enumerate(files):
        if not os.path.isdir(file):
            postfix=os.path.splitext(file)[1]
            if not os.path.exists(path+'/'+com_name+str(index)+postfix):
                os.rename(path+'/'+file,path+'/'+com_name+str(index)+postfix)

    print('重命名成功')
changeName('./imgDir','animal')