import os
def rename():
path = '/home/sy/CUB_200_2011/pytorch/train'
filelist = os.listdir(path)#该文件夹下所有文件(包括文件夹)
for files in filelist :#遍历所有文件
olddir = os.path.join(path,files)#原来的文件路径
# print(olddir)
if os.path.isdir(olddir):
filename = os.path.splitext(files)[0]#文件名151
os.rename(os.path.join(path,files),os.path.join(path,filename))#把文件路径重命名
# print(os.path.join(path,filename))#输出的是path+151
# print(newdir)
path1 = os.path.join(path,filename)#改完名以后的完整路径
print(path1)
# path1 = str(path1)
filelist1 =os.listdir(path1)#该文件夹下所有文件(包括文件夹)
# print(filelist1)
for files1 in filelist1:#遍历所有文件
# files1 = str(files1)
olddir1 = os.path.join(path1,files1)#原来的文件路径
# print(olddir1[0])
filename1=os.path.splitext(files1)[0]#文件名
print(filename1)
filetype1=os.path.splitext(files1)[1]#文件扩展名
# print(filetype1)
if filename1.find('_')>=0:#如果文件名中含有_
filename1=filename1.split('_')
# print(filename1[-1])#取最后一个
# print(filename1[-2])#取倒数第二个
newdir1=os.path.join(path1,filename+'_'+filename1[-2]+'_'+filename1[-1]+filetype1)
os.rename(os.path.join(path1,files1),os.path.join(path1,filename+'_'+filename1[-2]+'_'+filename1[-1]+filetype1))
print(newdir1)
rename();
def rename():
path = '/home/sy/CUB_200_2011/pytorch/train'
filelist = os.listdir(path)#该文件夹下所有文件(包括文件夹)
for files in filelist :#遍历所有文件
olddir = os.path.join(path,files)#原来的文件路径
# print(olddir)
if os.path.isdir(olddir):
filename = os.path.splitext(files)[0]#文件名151
os.rename(os.path.join(path,files),os.path.join(path,filename))#把文件路径重命名
# print(os.path.join(path,filename))#输出的是path+151
# print(newdir)
path1 = os.path.join(path,filename)#改完名以后的完整路径
print(path1)
# path1 = str(path1)
filelist1 =os.listdir(path1)#该文件夹下所有文件(包括文件夹)
# print(filelist1)
for files1 in filelist1:#遍历所有文件
# files1 = str(files1)
olddir1 = os.path.join(path1,files1)#原来的文件路径
# print(olddir1[0])
filename1=os.path.splitext(files1)[0]#文件名
print(filename1)
filetype1=os.path.splitext(files1)[1]#文件扩展名
# print(filetype1)
if filename1.find('_')>=0:#如果文件名中含有_
filename1=filename1.split('_')
# print(filename1[-1])#取最后一个
# print(filename1[-2])#取倒数第二个
newdir1=os.path.join(path1,filename+'_'+filename1[-2]+'_'+filename1[-1]+filetype1)
os.rename(os.path.join(path1,files1),os.path.join(path1,filename+'_'+filename1[-2]+'_'+filename1[-1]+filetype1))
print(newdir1)
rename();