由于https://blog.csdn.net/weixin_42630613/article/details/107835244是隔帧编号,最后将其混合在一起,现在想恢复混合之前的模样
import os
path = "/home/ubuntu/数据集"
path1 = "/home/ubuntu/数据集1"
# 获取该目录下所有文件,存入列表中
f = os.listdir(path)
print(len(f))
print(f[0])
n = 0
i = 0
for i in f:
print("i:",i)
if (int(i.split(".")[0])+11)%13==0:
# 设置旧文件名(就是路径+文件名)
oldname = i
one = path + "/" + oldname
two = path1 + "/" +"12" + "/" + oldname
# 用os模块中的rename方法对文件改名
os.rename(one, two)
print(one, '======>', two)
if (int(i.split(".")[0])+10)%13==0:
# 设置旧文件名(就是路径+文件名)
oldname = i
one = path + "/" + oldname
two = path1 + "/" +"11" + "/" + oldname
# 用os模块中的rename方法对文件改名
os.rename(one, two)
print(one, '======>', two)
if (int(i.split(".")[0])+9)%13==0:
# 设置旧文件名(就是路径+文件名)
oldname = i
one = path + "/" + oldname
two = path1 + "/" +"10" + "/" + oldname
# 用os模块中的rename方法对文件改名
os.rename(one, two)
print(one, '======>', two)
if (int(i.split(".")[0])+8)%13==0:
# 设置旧文件名(就是路径+文件名)
oldname = i
one = path + "/" + oldname
two = path1 + "/" +"9" + "/" + oldname
# 用os模块中的rename方法对文件改名
os.rename(one, two)
print(one, '======>', two)
if (int(i.split(".")[0])+7)%13==0:
# 设置旧文件名(就是路径+文件名)
oldname = i
one = path + "/" + oldname
two = path1 + "/" +"8" + "/" + oldname
# 用os模块中的rename方法对文件改名
os.rename(one, two)
print(one, '======>', two)
if (int(i.split(".")[0])+6)%13==0:
# 设置旧文件名(就是路径+文件名)
oldname = i
one = path + "/" + oldname
two = path1 + "/" +"7" + "/" + oldname
# 用os模块中的rename方法对文件改名
os.rename(one, two)
print(one, '======>', two)
按另一个文件夹内的文件批量修改名称
def changefilename():
import os.path
import xml.dom.minidom
xmlFilePath = r"C:\Users\D\Desktop\beifen1" # 首先将xml文件放到指定的(模型训练时代码读取的地方)文件夹内
imgPath = r"C:\Users\D\Desktop\beifen2"
xmlFileNames = os.listdir(xmlFilePath)
imgPaths = os.listdir(imgPath)
n = 0
for xmlFileName in xmlFileNames:
newname = xmlFileName.split(".")[0]
oldname = imgPaths[n]
os.rename(imgPath + "\\" + oldname, imgPath + "\\" + newname + ".xml")
print(oldname, '======>', newname)
n += 1