猿问

如何从目录列表中读取文件?

这里我有很多耐心折叠,每个折叠有 10 个图像。我有一个列表,其中存储了我想要的一些目录。


如何仅从列表(data_paths)上的目录加载/读取文件?


# data_paths is the list

data_paths = ['./data/preprocessed_data/train/Patient009969', './data/preprocessed_data/train/Patient009928', './data/preprocessed_data/train/Patient009966'] 

data_and_seg_arr = [np.load(ii, mmap_mode='r') for ii in data_paths]



Eror:

IsADirectoryError: [Errno 21] Is a directory: './data/preprocessed_data/train/Patient009969'

我需要保留这个数据表格。因为我将不得不这样做:


pids = [ii.split('/')[-1].split('.')[0] for ii in data_paths]

data = OrderedDict()

for ix, pid in enumerate(pids):

    data[pid] = {'data': data_and_seg_arr[ix][..., 0], 'seg': data_and_seg_arr[ix][..., 1], 'pid': pid}

return data


慕村9548890
浏览 163回答 3
3回答

慕桂英4014372

假设所有文件都是 jpg 或 png 格式path="/path/to/image"#NOTE THIS IS REALLY IMPORTANT#IF YOUR PATH IS LIKE THIS /path/to/image YOU NEED TO ADD A / AT THE END, IF&nbsp;#YOUR PATH ALREADY HAVE THE "/" AT THE END YOU DON'T NEED TO ADD IF#EXAMPLE PROBLEM PATH:/path/to/image#PATH WITH NO PROBLEM /path/to/image/jpg = glob.glob(path + "/*.jpg")png = glob.glob(path + "/*.png")imagelist=jpg+png>>>print(type(imagelist))<class 'list'>希望这能解决您的问题

慕田峪7331174

您需要创建另一个循环,遍历路径列表中的文件。import osfor path in data_paths:&nbsp; &nbsp; &nbsp; for file in os.listdir(path):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;data_and_seg_arr = [np.load(file, ...
随时随地看视频慕课网APP

相关分类

Python
我要回答