close

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

Today We are Going To Solve “IsADirectoryError: [Errno 21] Is a directory: It is a file” in Python. Here we will Discuss All Possible Solutions and How this error Occurs So let’s get started with this Article.

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

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

    To Fix “IsADirectoryError: [Errno 21] Is a directory: It is a file” Error just Use os.walk() function. Just use os.walk() function to solve this issue. data_paths = [os.path.join(pth, f) [for pth, dirs, files in os.walk(in_dir) for f in files]

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

    To Fix “IsADirectoryError: [Errno 21] Is a directory: It is a file” Error just Run the commands. Just run the below command to solve your issue. 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)]

Solution 1 : Use os.walk() function

Just use os.walk() function to solve this issue.

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

Solution 2 : Run the commands

Just run the below command to solve your issue.

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

Conclusion

So these were all possible solutions to this error. I hope your error has been solved by this article. In the comments, tell us which solution worked? If you liked our article, please share it on your social media and comment on your suggestions. Thank you.

Also Read These Solutions

Leave a Comment