How to find the parent directory of a given path using the path package?

To find the parent directory of a given path using the path package in Python, you can use the Path.parent attribute. Here's an example:

from pathlib import Path # Specify the path given_path = '/path/to/some/file.txt' # Create a Path object from the given path path_obj = Path(given_path) # Get the parent directory of the path parent_dir = path_obj.parent # Print the parent directory print(parent_dir)

In the above code, the Path(given_path) creates a Path object from the given path. Then, accessing the parent attribute of the Path object using path_obj.parent gives you the parent directory.

The output will be the parent directory of the given path (/path/to/some in this example).