How to determine if a path is a directory or a file using the path package?

To determine if a path is a directory or a file using the path package in Python, you can use the Path.is_dir() and Path.is_file() methods.

Here is an example:

from pathlib import Path path = Path("/path/to/directory_or_file") # Check if the path is a directory if path.is_dir(): print("Path is a directory.") # Check if the path is a file elif path.is_file(): print("Path is a file.") # Path does not exist or is neither a file nor a directory else: print("Path does not exist or is neither a file nor a directory.")

In this example, replace /path/to/directory_or_file with the actual path you want to check. The Path object is created from the path string, and then you can use the methods is_dir() and is_file() to determine if it is a directory or a file respectively. If the path does not exist, or if it is neither a directory nor a file, you can handle that case separately.