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:
pathlib
module:from pathlib import Path
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.
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
.