Python "IsADirectoryError: [Errno 21] Is a directory: " It is a file
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52338706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"IsADirectoryError: [Errno 21] Is a directory: " It is a file
提问by Zhuo
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.
我已经将数据分成测试集和训练集到不同的文件夹中。现在我需要加载患者数据。每个患者有 8 个图像。
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
But, the error said:
但是,错误说:
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!
它已经是一个文件名,而不是一个目录。谢谢!
回答by nosklo
It seems that ./data/preprocessed_data/train/Patient009969is a directory, not a file.
好像./data/preprocessed_data/train/Patient009969是目录,不是文件。
os.listdir()returns both files and directories.
os.listdir()返回文件和目录。
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:
也许尝试使用os.walk()代替。它将文件和目录分开处理,并且可以在子目录中递归以迭代方式查找更多文件:
data_paths = [os.path.join(pth, f)
for pth, dirs, files in os.walk(in_dir) for f in files]
回答by solarc
Do you have both files and directories inside your path? os.listdirwill list both files and directories, so when you try to open a directory with np.loadit will give that error. You can filter only files to avoid the error:
你的路径中有文件和目录吗?os.listdir将列出文件和目录,因此当您尝试使用np.load它打开目录时会出现该错误。您可以仅过滤文件以避免错误:
data_paths = [os.path.join(in_dir, f) for f in os.listdir(in_dir)]
data_paths = [i for i in data_paths if os.path.isfile(i)]
Or all together in a single line:
或者全部在一行中:
data_paths = [i for i in (os.path.join(in_dir, f) for f in os.listdir(in_dir)) if os.path.isfile(i)]
回答by Sirine Attia
I had the same problem but i resolved by changing my path from Data/Train_Data/myDataset/(my images) to Data/Train_Data/(my images) where the script python is in the same path as Data. Hope this help.
我遇到了同样的问题,但我通过将路径从 Data/Train_Data/myDataset/(my images) 更改为 Data/Train_Data/(my images) 来解决,其中脚本 python 与 Data 位于同一路径中。希望这有帮助。

