这篇文章将为大家详细讲解有关python如何实现遍历文件夹图片并重命名,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
在做深度学习相关项目时,需要标注图片、筛选过后图片名字带有括号,显得比较乱,因此利用python进行统一规范重命名操作
#, - *安康;编码:utf8 - * - , import 操作系统 , class 款小巧(): ,& # 39;& # 39;& # 39; ,批量重命名文件夹中的图片文件 ,& # 39;& # 39;& # 39; 自我,def __init__ ():=,self.path & # 39; C:/用户/联想/桌面/label5/3& # 39; , ,def 重命名(自我):=,,filelist  os.listdir (self.path)=,total_num , len(文件列表) ,小姐:=1 ,for item 拷贝文件列表: ,if item.endswith (& # 39; jpg # 39;):=,,src  os.path.join (os.path.abspath (self.path),项目)=,,dst  os.path.join (os.path.abspath (self.path), & # 39; 02 & # 39; +, str (i), +, & # 39; jpg # 39;) ,试一试: os.rename才能(src, dst) print 才能;& # 39;converting % s 用% s ……& # 39;, %, (src, dst) group 才能=0 ,除了: ,继续 ,if item.endswith (& # 39; xml # 39;):=,,src  os.path.join (os.path.abspath (self.path),项目)=,,dst  os.path.join (os.path.abspath (self.path), & # 39; 02 & # 39; +, str (i), +, & # 39; xml # 39;) ,试一试: os.rename才能(src, dst) print 才能;& # 39;converting % s 用% s ……& # 39;, %, (src, dst) group 才能=1 ,, ,除了: ,继续 ,if 组:=,小姐,小姐:+ 1 ,print & # 39; total % d 用rename ,, converted % d jpg # 39;, %, (total_num,,我) , if __name__ ==, & # 39; __main__ # 39;:=,,demo 款小巧() 之前,demo.rename () >
运行后效果如下
#, - *安康;编码:utf8 - * - import 操作系统 时间=path & # 39; SegmentationClassRaw/& # 39; 时间=filelist os.listdir(路径) for item 拷贝文件列表: ,#打印(& # 39;item name is & # 39;,项) ,if item.endswith (& # 39; . png # 39;):=,,name  item.split(& # 39;强生# 39;3)[0],+,& # 39;强生# 39;,+,item.split(& # 39;强生# 39;3)[1]=,,src  os.path.join (os.path.abspath(路径),项目)=,,dst  os.path.join (os.path.abspath(路径),name +, & # 39; . png # 39;) ,试一试: ,os.rename (src, dst) ,打印(& # 39;rename 得到% s 用% & # 39;% (src, dst)) ,除了: ,继续
关于“python如何实现遍历文件夹图片并重命名”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看的到。