. Advertisement .
..3..
. Advertisement .
..4..
Are you having troubles with the error IsADirectoryError: [Errno 21] Is a directory in Python? Don’t worry! We are here to help you. In this article, we give you some solutions to fix this problem. Read on it.
How Does The Error “IsADirectoryError: [Errno 21] Is a directory” In Python Happen?
When running your program, you easily get the following error:
IsADirectoryError: [Errno 21] Is a directory
How To Solve The Error “IsADirectoryError: [Errno 21] Is a directory” In Python?
Solution 1: Utilize os.walk() function
The simplest solution to fix the error “IsADirectoryError: [Errno 21] Is a directory” in Python is utilizing os.walk() function.
OS.walk() walks a top-down or bottom-up directory tree to create the file names. It produces a 3-tuple (dirpath, dirnames, dirpath filenames) for each directory in the tree that is rooted at the directory top (including the top itself).
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 2: Utilize os.path.isfile() in an if / else statement
You can check the directory first if you are not expecting a file, that is, if receiving a directory instead of a file isn’t an error state in your program. To do this, you can use the os.path.isfile() method as shown the following example:
import os
file_to_remove = 'testdir'
if os.path.isfile(file_to_remove):
os.remove(file_to_remove)
else:
print(f"Error: {file_to_remove} is not a file!")
# -> Error message: testdir is not a file!
Solution 3: Delete an empty directory with os.rmdir()
Another solution to fix the error “IsADirectoryError: [Errno 21] Is a directory” in Python is to delete an empty directory with os.rmdir() method. You need to do as the following first:
import os
os.rmdir('testdir')
This method is only effective if the directory is actually an empty.
Solution 4: Delete a non-empty directory with shutil.rmtree()
Take a look at the following directory structure:
.
├── main.py
└── testdir
└── test.txt
1 directory, 2 files
You must use shutil.rmtree() method like the following if you want to delete testdir/ and anything within of it (in this case, simply test.txt):
import shutil
shutil.rmtree('testdir')
There are many ways to delete everything in a directory, but this method is the simplest. Alternatively, you could retrieve every file in that directory and loop through each one individually. This would be the best solution if you needed to delete specific files or perform other processing on the files first. Otherwise, continue using shutil. rmtree().
Conclusion
You can always read this post again when you’re still stuck on IsADirectoryError: [Errno 21] Is a directory in Python. The options mentioned above are the quickest. In addition, if you still need help or have problems, we have a large population where everyone is usually eager to help. Finally, we wish all of you a wonderful day full of new code alternatives and appreciate your reading time.
Read more
Leave a comment