"IsADirectoryError:[Errno 21]是目录:"这是一个文件 [英] "IsADirectoryError: [Errno 21] Is a directory: " It is a file

查看:11163
本文介绍了"IsADirectoryError:[Errno 21]是目录:"这是一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将数据分为测试和培训集,并放入了不同的文件夹.现在,我需要加载患者数据.每个病人有8张图像.

I already split the data into test and training set into the different folder. Now I need to load the patient data. Each patient has 8 images.

def load_dataset(root_dir, split):
"""
load the data set numpy arrays saved by the preprocessing script
:param root_dir: path to input data
:param split: defines whether to load the training or test set
:return: data: dictionary containing one dictionary ({'data', 'seg', 'pid'}) per patient
"""
in_dir = os.path.join(root_dir, split)
data_paths = [os.path.join(in_dir, f) for f in os.listdir(in_dir)]
data_and_seg_arr = [np.load(ii, mmap_mode='r') for ii in data_paths]
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

但是,错误提示:

File "/home/zhe/Research/Seg/heart_seg/data_loader.py", line 61, in load_dataset
data_and_seg_arr = [np.load(ii, mmap_mode='r') for ii in data_paths]
File "/home/zhe/Research/Seg/heart_seg/data_loader.py", line 61, in <listcomp>
data_and_seg_arr = [np.load(ii, mmap_mode='r') for ii in data_paths]
File "/home/zhe/anaconda3/envs/tf_env/lib/python3.6/site-packages/numpy/lib/npyio.py", line 372, in load
fid = open(file, "rb")
IsADirectoryError: [Errno 21] Is a directory: './data/preprocessed_data/train/Patient009969'

它已经是文件名,而不是目录.谢谢!

It is already a file name, not a directory. Thanks!

推荐答案

似乎./data/preprocessed_data/train/Patient009969是目录,而不是文件.

It seems that ./data/preprocessed_data/train/Patient009969 is a directory, not a file.

os.listdir()返回文件和目录.

也许尝试使用os.walk()代替.它分别处理文件和目录,并可以在子目录内递归以迭代方式查找更多文件:

Maybe try using os.walk() instead. It treats files and directories separately, and can recurse inside the subdirectories to find more files in a iterative way:

data_paths = [os.path.join(pth, f) 
    for pth, dirs, files in os.walk(in_dir) for f in files]

这篇关于"IsADirectoryError:[Errno 21]是目录:"这是一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆