python批量修改文件名称

# -*- coding: utf-8 -*-    
#  
     批量修改文件名称  
     author:penelope  
#  
import os    
import cv2    
num=0    
path = '/home/penelope/picture/desktop/'                       #需要修改的图像文件路径    
path2='/home/penelope/picture/55/'                              #目标图像文件路径    
for file in os.listdir(path):                                   #读取路径下的所有文件    
    if os.path.isfile(os.path.join(path,file))==True:           #判断文件路径是否正确    
        address="%(path2)s/%(num)s.jpg"                         #组合目标文件全名称    
        while os.path.exists(address%{'path2':path2,'num':str(num)}): #如果目标已经含有此文件    
            num=num+1                                                 #则标号加1    
            if not os.path.exists(address % {'path2': path2, 'num': str(num)}): #如果目标路径没有此文件,则确定使用此文件名    
                break    
    files=cv2.imread("/home/penelope/picture/desktop/%s"%(file),3)   #读取图像文件    
    cv2.imwrite(address % {'path2': path2, 'num': str(num)}, files)  # 保存图像在path2    
    num=num+1    

猜你喜欢

转载自blog.csdn.net/Lxiaohuli/article/details/77497142