import os
from random import Random
def random_str():
randomlength=32
str = ''
chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
# chars='abcdefghijklmnopqrstuvwxyz0123456'
length = len(chars) - 1
random = Random()
for i in range(randomlength):
str+=chars[random.randint(0, length)]
return str
def NB():
outer_path = 'F:\\sizaif\\wallpaper'
folderlist = os.listdir(outer_path) # 列举文件夹
for folder in folderlist:
inner_path = os.path.join(outer_path, folder)
total_num_folder = len(folderlist) # 文件夹的总数
print("total have %d folders" % (total_num_folder))
filelist = os.listdir(inner_path) # 列举图片
i = 0
for item in filelist:
total_num_file = len(filelist) # 单个文件夹内图片的总数
if item.endswith('.jpg'):
src = os.path.join(os.path.abspath(inner_path), item) # 原图的地址
dst = os.path.join(os.path.abspath(inner_path), str(random_str()) + '.jpg')
# 新图的地址(这里可以把str(folder) + '_' + str(i) + '.jpg'改成你想改的名称)
try:
os.rename(src, dst)
print
'converting %s to %s ...' % (src, dst)
i += 1
except:
continue
elif item.endswith('.png'):
src = os.path.join(os.path.abspath(inner_path), item) # 原图的地址
dst = os.path.join(os.path.abspath(inner_path), str(random_str()) + '.png')
# 新图的地址(这里可以把str(folder) + '_' + str(i) + '.jpg'改成你想改的名称)
try:
os.rename(src, dst)
print
'converting %s to %s ...' % (src, dst)
i += 1
except:
continue
print("/*****")
print(inner_path+" has done !!!")
if __name__ == '__main__':
NB()