1、在windows 上
#/usr/bin/python #-*-coding:utf-8-*- # 把名字修改并排序 import os from sys import argv from nt import chdir sortId = 1 #刚开始从000001开始 # globalpath = 'E:\Python\\LPTHW\\ExeRecord\\fastercn\\xml' globalpath = 'D:\PycharmProjects\python35\Modify-Sun\\picpic' def renameF(preName, newName): chdir(os.path.dirname(preName)) os.rename(preName, newName) #for root, dirs, files in os.walk(argv[1]): print(argv[1]) for root, dirs, files in os.walk(argv[1]): # print(files) #褰撳墠璺緞涓嬫墍鏈夐潪鐩綍瀛愭枃浠? for path in files: print (path) sortName = '%06d' % sortId # 表示是6位数 ,如:000001 fileName = '%s.jpg' % sortName # 000001.jpg ''' with open("xmllist.txt", "a") as f: des = '%s --> %s' % (path, fileName) f.write(des) f.write('\n') ''' src = globalpath + '\\' + path # path 原来的名字 dst = globalpath + '\\' + fileName #fileName 是新名字 print (src) print (dst) chdir(os.path.dirname(src)) # os.path.dirname(src) 是要切换到的新路径 ,用于改变当前工作目录到指定的路径。 renameF(src, dst) # 把src 重dst sortId += 1
2、在linux 上
#/usr/bin/python #-*-coding:utf-8-*- # 把名字修改并排序 import os from sys import argv #from nt import chdir sortId = 1 #刚开始从000001开始 globalpath = '/home/huoo/py-faster-rcnn/data/VOCdevkit2007/VOC2007/JPEGImages' print (globalpath ) def renameF(preName, newName): os.chdir(os.path.dirname(preName)) os.rename(preName, newName) #for root, dirs, files in os.walk(argv[1]): # for root, dirs, files in os.walk(globalpath): # print(files) #褰撳墠璺緞涓嬫墍鏈夐潪鐩綍瀛愭枃浠? list =os.listdir(globalpath) list.sort(key =lambda x:int(x[:-4])) print list for path in list: print (path) sortName = '%06d' % sortId # 表示是6位数 ,如:000001 fileName = '%s.jpg' % sortName # 000001.jpg ''' with open("xmllist.txt", "a") as f: des = '%s --> %s' % (path, fileName) f.write(des) f.write('\n') ''' src = globalpath + '/' + path # path 原来的名字 dst = globalpath + '/' + fileName #fileName 是新名字 print (src) print (dst) os.chdir(os.path.dirname(src)) # os.path.dirname(src) 是要切换到的新路径 ,用于改变当前工作目录到指定的路径。 renameF(src, dst) # 把src 重dst sortId += 1 print('exit')