I ran the following code to get the relative path from the absolute path
import pathlib
p = pathlib.Path()
file_path = 'image-db'
file_path_rel = p.cwd().relative_to(file_path)
error
ValueError: '/directory/of/python' does not start with 'image-db'
It seems that it will not work if the contents of relative_to () are outside the current directory.
You can also get complex relative paths by using os.path.reipath ('destination','starting point').
file_path_rel = os.path.relpath(file_path, os.getcwd())
https://hibiki-press.tech/python/os_path_abspath/1021
Recommended Posts