Check if a Given Path is a File or Directory in Python
In Python, you can check whether a given path is a file or a directory using the os.path or pathlib modules. The os.path.isfile() function returns True if the path is a file, while os.path.isdir() checks if it is a directory. Similarly, pathlib.Path() provides the .is_file() and .is_dir() methods for the same purpose. Below, we cover different ways to achieve this with examples.
Examples to Check if a Given Path is a File or Directory
1. Checking if a Given Path is a File or Directory using os.path
In this example, we will use the os.path.isfile() and os.path.isdir() functions to check whether a given path 'example.txt' is a file or a directory.

main.py
import os
# Define a path
path = "example.txt"
# Check if the path is a file
if os.path.isfile(path):
print(f"'{path}' is a file.")
elif os.path.isdir(path):
print(f"'{path}' is a directory.")
else:
print(f"'{path}' does not exist.")
Explanation:
- We import the
osmodule to work with file system paths. - We define a variable
pathand assign it a sample file or directory name. - The function
os.path.isfile(path)checks if the given path is a file. - The function
os.path.isdir(path)checks if the given path is a directory. - If neither condition is met, the path does not exist.
Output:
'example.txt' is a file.
2. Checking if a Given Path is a File or Directory using pathlib
In this example, we will use the pathlib.Path module, which provides an object-oriented approach to handle file paths.
We have just created example_dir directory next to out main.py Python script file.

main.py
from pathlib import Path
# Define a path
path = Path("example_dir")
# Check if the path is a file
if path.is_file():
print(f"'{path}' is a file.")
elif path.is_dir():
print(f"'{path}' is a directory.")
else:
print(f"'{path}' does not exist.")
Explanation:
- We import the
Pathclass from thepathlibmodule. - We define a variable
pathand assign it a directory name. - The method
path.is_file()checks if the given path is a file. - The method
path.is_dir()checks if the given path is a directory. - If neither condition is met, the path does not exist.
Output:
'example_dir' is a directory.
3. Checking a User-Provided Path
In this example, we ask the user to input a file or directory path and determine its type.
main.py
import os
# Get user input for the path
user_path = input("Enter a file or directory path: ")
# Check if the path is a file or directory
if os.path.isfile(user_path):
print(f"'{user_path}' is a file.")
elif os.path.isdir(user_path):
print(f"'{user_path}' is a directory.")
else:
print(f"'{user_path}' does not exist.")
Explanation:
- We use
input()to get a file or directory path from the user. - The function
os.path.isfile()checks if the entered path is a file. - The function
os.path.isdir()checks if the entered path is a directory. - If neither check is satisfied, we inform the user that the path does not exist.
Output:



Conclusion
In conclusion, to check if a given path is a file or directory in Python, we can use:
os.path.isfile()andos.path.isdir(): These functions from theosmodule check whether a path is a file or directory.pathlib.Path().is_file()andpathlib.Path().is_dir(): These methods provide an object-oriented approach to checking paths.- User Input: We can also check paths dynamically by getting user input.
The os module is useful for quick checks, while pathlib offers a more structured way to handle file paths.
