IsADirectoryError
Python Programming Language
Severity: MinorWhat Does This Error Mean?
An IsADirectoryError means your Python code tried to perform a file operation (such as opening for reading or writing) on a path that points to a directory, not a file. You cannot read from or write to a directory the same way you read from a file. The fix is to check your path — you either have the wrong path, or you need to point to a specific file inside that directory.
Affected Models
- Python 3.12
- Python 3.11
- Python 3.10
- Python 3.9
- Python 3.8
Common Causes
- Passing a directory path to open() when a file path was expected
- A trailing slash or backslash on a path, turning it into a directory reference
- A variable that was supposed to hold a filename ended up holding just the folder path
- Joining a directory path and forgetting to append the actual filename
- Reading user input for a file path and the user typed a directory instead of a file
How to Fix It
-
Print the path your code is using just before the line that causes the error. Confirm whether it is a directory or a file.
Add: print(type_of_path, my_path) before the open() call. Then check in your file explorer whether that path is a folder or a file.
-
If the path is a directory, append the filename to it. Use os.path.join() to safely combine a directory path and a filename.
Example: 'filepath = os.path.join(directory, filename)'. Never concatenate paths with + — os.path.join() handles separators correctly on all operating systems.
-
Check for accidental trailing slashes in your path. A path like '/home/user/data/' is a directory reference, while '/home/user/data' could be a file.
Use os.path.normpath(path) to clean up trailing slashes, or use pathlib's Path object which handles this automatically.
-
Use os.path.isfile() to verify that a path points to a file before opening it. This makes your code safer and the error message clearer.
Example: 'if os.path.isfile(path): open it, else: print("path is not a file")'. Use os.path.isdir(path) to check if it is a directory.
-
If the path comes from user input or a configuration file, validate it before using it. Check that it refers to a file and that the file exists.
Combining os.path.exists() and os.path.isfile() gives you a complete check before attempting to open anything.
When to Call a Professional
IsADirectoryError is always fixable by correcting the path in your code. If you are dynamically building paths and cannot see why a directory path is being used, add print statements to show the exact path at runtime before the operation. Stack Overflow has many examples of correct path building with os.path.join() and pathlib.
Frequently Asked Questions
How do I list all files in a directory instead of trying to open the directory itself?
Use os.listdir(directory_path) to get a list of all items in a directory. Then loop over the list and use os.path.join(directory_path, item) to get the full path to each file. You can then open each file individually with open().
What is the opposite error — trying to use a file as a directory?
The opposite is NotADirectoryError, which you get when you try to use a file path where a directory is expected. For example, calling os.listdir() on a file path raises NotADirectoryError. Both errors come from the same root cause: using the wrong kind of path for the operation.
Can I use pathlib instead of os.path to avoid these errors?
Yes — pathlib is the modern, recommended way to work with file paths in Python. Path objects make it easy to check .is_file(), .is_dir(), and to join paths cleanly with the / operator. Many developers find pathlib cleaner and less error-prone than os.path functions.