Check If a File/Directory Exists in Python: os.path.exists, isfile, isdir

Modified: | Tags: Python, File

In Python, you can check if a file or directory (folder) exists using os.path.exists(), os.path.isfile(), and os.path.isdir(). The pathlib module, which manipulates paths as objects, has similar methods.

Each function accepts a path-like object, such as a path string or a pathlib.Path object, as an argument.

In this article, we use the following three path strings: an existing file path, an existing directory path, and a non-existent path.

import os

file_path = 'data/temp/dir/file.txt'
dir_path = 'data/temp/dir'
non_existent_path = 'non_exist'

Note that the presence or absence of the trailing separator / in directory paths does not affect their recognition.

For information on manipulating path strings, see the following article.

Check if a path exists with os.path.exists()

To check if a path exists, use os.path.exists().

It returns True if the specified path exists, whether it is a file or a directory. If the path does not exist, it returns False.

print(os.path.exists(file_path))
# True

print(os.path.exists(dir_path))
# True

print(os.path.exists(non_existent_path))
# False

Check if a file exists with os.path.isfile()

To check if a path is an existing file, use os.path.isfile().

If the specified path is an existing file, it returns True. If the path represents a directory, it returns False, even if it exists.

print(os.path.isfile(file_path))
# True

print(os.path.isfile(dir_path))
# False

print(os.path.isfile(non_existent_path))
# False

It correctly recognizes and determines file names without extensions, such as Makefile.

Check if a directory exists with os.path.isdir()

To check if a path is an existing directory, use os.path.isdir().

If the specified path is an existing directory, it returns True. If the path represents a file, it returns False, even if it exists.

print(os.path.isdir(file_path))
# False

print(os.path.isdir(dir_path))
# True

print(os.path.isdir(non_existent_path))
# False

Trailing separator in directory paths

In the examples provided so far, we have used the directory path without a trailing separator /. However, the results are the same whether the trailing separator is present or not.

dir_path_with_sep = 'data/temp/dir/'

print(os.path.exists(dir_path_with_sep))
# True

print(os.path.isfile(dir_path_with_sep))
# False

print(os.path.isdir(dir_path_with_sep))
# True

For the pathlib module

Similar checks can also be performed with the pathlib module, which manipulates paths as objects. For the basics of the pathlib module, see the following article.

Consider the following Path objects as examples.

from pathlib import Path

p_file = Path('data/temp/dir/file.txt')
p_dir = Path('data/temp/dir')
p_non_existent = Path('non_exist')

Use the exists(), is_file(), and is_dir() methods on a Path object.

To check the existence of a path, whether it is a file or a directory, use the exists() method.

print(p_file.exists())
# True

print(p_dir.exists())
# True

print(p_non_existent.exists())
# False

To check for the existence of a file, use the is_file() method.

print(p_file.is_file())
# True

print(p_dir.is_file())
# False

print(p_non_existent.is_file())
# False

To check for the existence of a directory, use the is_dir() method.

print(p_file.is_dir())
# False

print(p_dir.is_dir())
# True

print(p_non_existent.is_dir())
# False

When creating a Path object representing a directory, a trailing separator / is optional.

print(Path('data/temp/dir') == Path('data/temp/dir/'))
# True

Related Categories

Related Articles