参考简易版,单层文件夹的遍历访问:
rootdir = 'F:\data'
list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i])
if os.path.isfile(path):
#你想对文件的操作
下面是双层文件夹的访问:
import os
import cv2
data_path = '/home/xiqi/PycharmProjects/FBMS/Dataset/Trainingset/'
video_list = os.listdir(data_path)
save_path = './SaliencyMap/'
if not os.path.exists(save_path): # 文件夹不存在,则创建
os.mkdir(save_path)
for i in range(0, len(video_list)):
print(i)
video_path = os.path.join(data_path, video_list[i]) # 视频文件夹
if os.path.isdir(video_path):
img_list = os.listdir(video_path)
save_video_path = os.path.join(save_path, video_list[i])
if not os.path.exists(save_video_path):
os.mkdir(save_video_path)
for j in range(0, len(img_list)):
print(j)
img_path = os.path.join(video_path, img_list[j]) # 图片文件
if os.path.isfile(img_path):
# salmap = processImage(net, img_path) # 操作
save_img_path = os.path.join(save_video_path, img_list[j])
cv2.imwrite(save_img_path, salmap)