1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time 2021/3/13 10:21
# @Author latin-xiao-mao
# @Version v1.0
# @File BatchRename.py
import os
import random
class BatchRename():
def __init__(self):
self.path = 'E:/github-project/18x-images/' # 表示需要命名处理的文件夹
print(self.path)
def rename(self):
files = os.listdir(self.path) # 获取文件路径
random.shuffle(files)
print(files)
total_num = len(files) # 获取文件长度(个数)
# new_filepath='/home/tanBin/deepLearning/python_learning/caiLearning/crawl_picture/tmp/'
# if not os.path.exists(new_filepath):
# os.makedirs(new_filepath)
i = 202101 # 图片开始重命名的名称
for item in files:
# print item
if item.endswith('.jpg') or item.endswith('.webp') or item.endswith('.png'): # 源文件是png格式及其他格式
src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), format(str(i), '0>6s') + '.jpg') # 处理后的格式也为jpg格式的
os.rename(src, dst)
print('converting %s to %s ...' % (src, dst))
i = i + 1
print('total %d to rename & converted %d jpgs' % (total_num, i))
if __name__ == '__main__':
newName = BatchRename()
newName.rename()
|