# coding = 'utf-8'
import os
def rename(path):
i = 0
'該文件夾下所有的文件(包括文件夾)'
FileList = os.listdir(path)
'遍歷所有文件'
for files in FileList:
'原來的文件路徑'
oldDirPath = os.path.join(path, files)
'如果是文件夾則遞歸調用'
if os.path.isdir(oldDirPath):
rename(oldDirPath)
'文件名'
fileName = os.path.splitext(files)[0]
'文件擴展名'
fileType = os.path.splitext(files)[1]
'新的文件路徑'
newDirPath = os.path.join(path, str(i) + fileType)
'重命名'
os.rename(oldDirPath, newDirPath)
i += 1
path = 'E:/baidu_img/柱上變壓器_img1'
rename(path)
方式二:
import os
path = 'E:\ImgNew'
files = os.listdir(path)
for i, file in enumerate(files):
NewFileName = os.path.join(path, str(i)+'.jpg')
OldFileName = os.path.join(path, file)
os.rename(OldFileName, NewFileName)