''' author : Hatcher W Email: hcwang@semi.ac.cn use: 递归一个文件下面所有的视频和图像的路径 input:一个文件夹目录 output:两个json文件,分别存储视频和图像的路径 ''' import os import shutil import json vediofile = [] imagefile = [] vedio_format = [".MP4" , ".mp4" , ".MOV" , ".mov"] image_format = [".JPG" , ".JPEG" , ".jpg"] def eachfile(filepath): pathdir = os.listdir(filepath) for s in pathdir: newdir = os.path.join(filepath,s)#将文件名加到当前文件路径后面 if os.path.isfile(newdir): #判断是不是文件 print(os.path.splitext(newdir)[1]) if os.path.splitext(newdir)[1] in vedio_format: vediofile.append(newdir) elif os.path.splitext(newdir)[1]in image_format: imagefile.append(newdir) elif os.path.isdir(newdir): eachfile(newdir) else: pathdir_1 = os.listdir(newdir) #取出下一层的路径 for s_1 in pathdir_1: #遍历下一层的路径 newdir_1 = os.path.join(newdir,s_1) #取出其中一个路径 if os.path.isfile(newdir_1): # 判断是不是文件 print(os.path.splitext(newdir_1)[1]) if os.path.splitext(newdir_1)[1] in vedio_format: vediofile.append(newdir_1) elif os.path.splitext(newdir_1)[1] in image_format: imagefile.append(newdir_1) elif os.path.isdir(newdir_1): eachfile(newdir_1) else:eachfile(newdir_1) return vediofile,imagefile fp = "/data1/Hatcher/DATA/Hat_Raw_Data" vedio_f,image_f = eachfile(fp) print(vedio_f) print(image_f) with open ("./vedioroot.json",'w') as f: json.dump(vedio_f,f) f.close() with open ("./imageroot.json",'w') as g: json.dump(image_f,g) g.close() for i in range(len(vedio_f)): pcm_file = vedio_f[i] path_0 = os.path.split(vedio_f[i])[0] path_1 = os.path.split(vedio_f[i])[1] #print(path_1)