# 获取文件夹路径
def get_dirs(dir):
Dirlist = []
for root, dirs, files in os.walk(dir):
for d in dirs:
Dirlist.append(os.path.join(root, d))
return Dirlist
# 获取文件的路径
def get_files(dir):
Filelist = []
for root, dirs, files in os.walk(dir):
for f in files:
Filelist.append(os.path.join(root, f))
return Filelist
# 将名字中带picture的都换成文件夹的名字
def Rename(path):
'''
:param path: 文件目录
:return:
'''
dirs = get_dirs(path) # 文件夹路径列表
for dir in dirs:
pics = get_files(dir) # 图片路径列表
for pic in pics: # 对每一个路径重命名
newname = pic.replace("picture", os.path.basename(dir))
os.rename(pic, newname)
# 将文件名字按顺序命名
def Rename_1(path):
dirs = get_dirs(path) # 文件夹路径列表
for dir in dirs:
pics = get_files(dir) # 图片路径列表
i = 0
# print(os.path.basename(dir))
for pic in pics:
os.chdir(path)
newname = pic.replace(pic, os.path.join(os.path.basename(dir)+'_'+ str(i) + ".jpg"))
print(os.path.join(os.path.abspath(dir),newname))
try:
os.rename(pic, os.path.join(os.path.abspath(dir),newname))
except:
pass
i += 1