how to check if a file is a directory or regular file in python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When Python code accepts a path from a user, config file, or command-line argument, you usually need to know whether that path points to a regular file or a directory. The cleanest solutions use either pathlib or os.path, and both work well as long as you understand how they behave for missing paths and symbolic links.
Use pathlib for New Code
pathlib.Path gives you an object-oriented API that is easy to read and hard to misuse. For most modern Python code, it should be the default choice.
is_file() returns True for a regular file. is_dir() returns True for a directory. If the path does not exist, both methods return False.
Here is a reusable helper that classifies a path more explicitly:
That version is useful because real-world filesystem checks are rarely binary. You may encounter sockets, device files, broken links, or paths that simply do not exist.
Use os.path in Older Codebases
If the project already relies on the os module, os.path.isfile() and os.path.isdir() are still perfectly valid.
This is functionally similar to the pathlib approach. The main difference is style: pathlib keeps path manipulation and inspection together on one object, which becomes clearer once you start joining paths or iterating directories.
Choose the Check That Matches the Task
In practice, the right check depends on what your code will do next. If you plan to open a configuration file, is_file() is the clearest guard. If you are about to iterate contents, is_dir() is the better fit. For command-line tools, it is often worth printing the resolved absolute path in error messages so users can immediately see which filesystem location your program actually examined.
When You Need More Detail
Sometimes you need to know not only whether a path is a file or directory, but also whether it is a symlink or what its exact mode bits are. In that case, inspect the path with stat.
This is more verbose, but it gives you low-level control. It is a good fit for tooling, deployment scripts, or validation code that has to distinguish among more than two filesystem types.
Common Pitfalls
- Assuming one of the checks must be true. For a missing path, both
is_file()andis_dir()returnFalse. - Forgetting about symbolic links. A symlink may point to a file or directory, so call
is_symlink()if that distinction matters. - Treating every existing path as a regular file. Filesystems can also contain sockets, named pipes, and device entries.
- Using relative paths without realizing the current working directory changed. Convert to an absolute path when debugging unexpected results.
- Ignoring permission problems in surrounding code. While these checks are straightforward, later operations such as opening the file may still fail.
Summary
- Use
pathlib.Path.is_file()andpathlib.Path.is_dir()for clear modern Python code. - Use
os.path.isfile()andos.path.isdir()when working in older code that already usesos. - Check
is_symlink()separately if link handling matters. - Expect both checks to return
Falsefor missing paths. - Use
statonly when you need low-level filesystem type details.

