python 重命名文件名

`#对于挑选出来的文件重新命名
import os
import shutil

def rename(images_path,xml_path):
xml_ = os.listdir(xml_path)
images_ = os.listdir(images_path)
i = 1
j=1
for item in images_:
item_new_name = item.replace(str(item),"%d.xml"%i)
item_new_name = item_new_name.zfill(9)
os.rename(os.path.join(images_path,item),os.path.join(images_path,item_new_name))
print(item_new_name)
i +=1
for itemimg in xml_:
itemimg_new_name = itemimg.replace(str(itemimg),"%d.jpg"%j)
itemimg_new_name = itemimg_new_name.zfill(9)#00000.jpg格式 用0填充前面
os.rename(os.path.join(xml_path,itemimg),os.path.join(xml_path,itemimg_new_name))
j +=1
print(itemimg_new_name)

if name == 'main':
xml_path = r'D:\Postgraduate_study_materials\teacher\ahu_vot\dior_process\DIOR\Annotations'
images_path = r'D:\Postgraduate_study_materials\teacher\ahu_vot\dior_process\DIOR\Images'
#images_path = r'D:\Postgraduate_study_materials\teacher\ahu_vot\dior_process\testpy\testimages'
rename(xml_path,images_path)
# rename(images_path)`

posted @ 2021-09-28 09:01  Maggieisxin  阅读(597)  评论(0编辑  收藏  举报