您的位置:首页 > 编程语言 > Python开发

python 给同一文件夹下 所有图片 批量改名

2018-01-19 09:38 393 查看
# -*- coding:utf8 -*-

import os

class BatchRename():
'''
批量重命名文件夹中的图片文件

'''
def __init__(self):
self.path = 'C:/Users/49691/Desktop/model/seborrheic'

def rename(self):
filelist = os.listdir(self.path)
total_num = len(filelist)
print(total_num)
i = 0
for item in filelist:
if item.endswith('.jpg'):
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), str(i) + '.jpg')
print(item,i)
try:
os.rename(src, dst)
print ("converting %s to %s ..." % (src, dst))
i = i + 1
except:
continue
print ("total %d to rename & converted %d jpgs" % (total_num, i))

if __name__ == '__main__':
demo = BatchRename()
demo.rename()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: