How to find the relative path from one directory to another using the path package?

To find the relative path from one directory to another using the path package in Python, you can use the pathlib module. Follow these steps:

  1. Import the pathlib module:
from pathlib import Path
  1. Get the absolute paths of the two directories:
dir1 = Path('/path/to/dir1') dir2 = Path('/path/to/dir2')

Replace /path/to/dir1 and /path/to/dir2 with the actual paths of your directories.

  1. Get the relative path between the two directories:
relative_path = dir1.relative_to(dir2)

This will give you the relative path from dir1 to dir2.

Here's a complete example:

from pathlib import Path dir1 = Path('/path/to/dir1') dir2 = Path('/path/to/dir2') relative_path = dir1.relative_to(dir2) print(relative_path)

This will print the relative path from dir1 to dir2.