python - IsADirectoryError: [Errno 21] Is a directory: It is a file

python – IsADirectoryError: [Errno 21] Is a directory: It is a file

python – IsADirectoryError: [Errno 21] Is a directory: It is a file

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

n

os.listdir() returns both files and directories.

n

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:

n

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

Do you have both files and directories inside your path? os.listdir will list both files and directories, so when you try to open a directory with np.load it will give that error. You can filter only files to avoid the error:

n

data_paths = [os.path.join(in_dir, f) for f in os.listdir(in_dir)]ndata_paths = [i for i in data_paths if os.path.isfile(i)]n

n

Or all together in a single line:

n

data_paths = [i for i in (os.path.join(in_dir, f) for f in os.listdir(in_dir)) if os.path.isfile(i)]n

python – IsADirectoryError: [Errno 21] Is a directory: It is a file

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.nHope this help.

Related posts on python :

Leave a Reply

Your email address will not be published.