在Insight-MVT_Annotation_Train文件夹下有60个子文件夹(子文件夹可以无规则命名)。现将每个子文件夹下的图片文件合并到同一个文件夹下(image)并重新给图片命名。
脚本代码如下:
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys
path1 = 'Insight-MVT_Annotation_Train' # 所需修改文件夹所在路径
dirs = os.listdir(path1)
count = 0
for dir in dirs:
new_path = 'image'
n = 0
for j in range(len(dirs)):
old_path = path1 + os.sep + dir
# 获取该目录下所有文件,存入列表中
fileList = os.listdir(old_path)
for i in fileList:
# 设置旧文件名(就是路径+文件名)
oldname = old_path + os.sep + i # os.sep添加系统分隔符
# print(oldname)
# 设置新文件名
newname = new_path + os.sep + dir + '_' + i
# print(newname)
os.rename(oldname, newname) # 用os模块中的rename方法对文件改名
print(oldname, '======>', newname)
n += 1
count += 1
print(dir+'重命名文件数:',n)
print('重命名文件总数:', count)