close

[Solved] “IsADirectoryError: [Errno 21] Is a directory: ” It is a file

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error “IsADirectoryError: [Errno 21] Is a directory: ” It is a file in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How “IsADirectoryError: [Errno 21] Is a directory: ” It is a file Error Occurs?

Today I get the following error “IsADirectoryError: [Errno 21] Is a directory: ” It is a file in python.

How To Solve “IsADirectoryError: [Errno 21] Is a directory: ” It is a file Error ?

  1. How To Solve “IsADirectoryError: [Errno 21] Is a directory: ” It is a file Error ?

    To Solve “IsADirectoryError: [Errno 21] Is a directory: ” It is a file Error It seems that ./data/preprocessed_data/train/Patient009969 is a directory, not a file.

  2. “IsADirectoryError: [Errno 21] Is a directory: ” It is a file

    To Solve “IsADirectoryError: [Errno 21] Is a directory: ” It is a file Error It seems that ./data/preprocessed_data/train/Patient009969 is a directory, not a file.

Solution 1

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

os.listdir() returns both files and directories.

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]

Solution 2

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:

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)]

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read