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

</>
Copy
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:

  1. We import the os module to work with file system paths.
  2. We define a variable path and assign it a sample file or directory name.
  3. The function os.path.isfile(path) checks if the given path is a file.
  4. The function os.path.isdir(path) checks if the given path is a directory.
  5. 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

</>
Copy
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:

  1. We import the Path class from the pathlib module.
  2. We define a variable path and assign it a directory name.
  3. The method path.is_file() checks if the given path is a file.
  4. The method path.is_dir() checks if the given path is a directory.
  5. 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

</>
Copy
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:

  1. We use input() to get a file or directory path from the user.
  2. The function os.path.isfile() checks if the entered path is a file.
  3. The function os.path.isdir() checks if the entered path is a directory.
  4. 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:

  1. os.path.isfile() and os.path.isdir(): These functions from the os module check whether a path is a file or directory.
  2. pathlib.Path().is_file() and pathlib.Path().is_dir(): These methods provide an object-oriented approach to checking paths.
  3. 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.