# -*- coding: utf-8 -*-
import os
path='./img_clear_2/'
#获取该目录下所有文件,存入列表中
f=os.listdir(path)
print(f)
counter = 0
for i in range(len(f)):
name_tmp = f[i]
print(name_tmp)
#设置旧文件名(就是路径+文件名)
oldname=path+f[counter]
#设置新文件名
counter += 1
if len(str(counter)) == 1:
file_name = "00000" + str(counter) + '.jpg'
if len(str(counter)) == 2:
file_name = "0000" + str(counter) + '.jpg'
if len(str(counter)) == 3:
file_name = "000" + str(counter) + '.jpg'
if len(str(counter)) == 4:
file_name = "00" + str(counter) + '.jpg'
if len(str(counter)) == 5:
file_name = "0"+ str(counter) + '.jpg'
if len(str(counter)) == 6:
file_name = str(counter) + '.jpg'
newname=path + file_name
#用os模块中的rename方法对文件改名
os.rename(oldname,newname)
print(oldname,'======>',newname)
网友评论