Python Glossary Русский

Python 3.3+Exceptionsexception

IsADirectoryError

built-in exceptions

A file operation expected a file but got a directory (a subclass of OSError).

Syntax

raise IsADirectoryError

It usually comes from open() or os.remove() handed a directory path instead of a file: directories need os.listdir(), os.rmdir() or shutil.rmtree(). The class is platform-dependent — on Windows the same attempt typically surfaces as PermissionError, so portable code catches the parent OSError or checks Path.is_file() up front.

Examples

try:
    raise IsADirectoryError
except IsADirectoryError as e:
    print(type(e).__name__)   # → IsADirectoryError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy