I got stuck when trying to specify a relative path with relative_to () in python

Overview

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.

Countermeasures

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())

Reference site

https://hibiki-press.tech/python/os_path_abspath/1021

Recommended Posts

I got stuck when trying to specify a relative path with relative_to () in python
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
A reminder of what I got stuck when starting Atcoder with python
I want to work with a robot in python.
What I did when I got stuck in the time limit with lambda python
When I made CaboCha usable with python3, I got stuck (Windows 10)
A story that got stuck when trying to upgrade the Python version on GCE
[python] A note when trying to use numpy with Cython
When creating a pipenv environment, I got addicted to "Value Error: Not a valid python path"
A story that went missing when I specified a path starting with a tilde (~) in python open
When I got a list of study sessions in Python, I found something I wanted to make
Read a file in Python with a relative path from the program
A story that didn't work when I tried to log in with the Python requests module
It was dangerous to specify a relative path when generating a symbolic link
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
I got stuck in a flask application redirect with a reverse proxy in between
A memorandum because I stumbled on trying to use MeCab in Python
Error when trying to install psycopg2 in Python
I want to write to a file with Python
I got stuck trying to install various things with Mac El captain pip, so make a note
I got an error when trying to run Hello World in Go language
When I cut the directory for UNIX Socket under / var / run with systemd, I got stuck in a pitfall and what to do
I got an error when I put opencv in python3 with Raspberry Pi [Remedy]
When I tried to create a virtual environment with Python, it didn't work
When I tried to connect with SSH, I got a warning about free space.
A note I was addicted to when running Python with Visual Studio Code
A story that I was addicted to when I made SFTP communication with python
I referred to it when I got stuck in the django geodjango tutorial (editing)
When I tried to use Python on WSL (windows subsystem for linux), it got stuck in Jupyter (solved)
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to transition with a button in flask
I tried to draw a route map with Python
I want to write in Python! (2) Let's write a test
I tried to implement a pseudo pachislot in Python
I want to randomly sample a file in Python
I was addicted to scraping with Selenium (+ Python) in 2020
Things to note when initializing a list in Python
I tried to automatically generate a password with Python3
Run a Python file with relative import in PyCharm
I want to run a quantum computer with Python
I want to do something in Python when I finish
A memo of misunderstanding when trying to load the entire self-made module with Python3
I got stuck doing Django1.4, so I wrote a "memo" about absolute path and relative path.
I got a Value Error when using JUMAN ++ with PyKNP
I made a simple typing game with tkinter in Python
I want to specify another version of Python with pyvenv
I tried to implement a one-dimensional cellular automaton in Python
What I got stuck around GUI in WSL python environment
I made a package to filter time series with python
I wrote a program quickly to study DI with Python ①
I tried "How to get a method decorated in Python"
I made a puzzle game (like) with Tkinter in Python
Things to keep in mind when using Python with AtCoder
I tried to make a stopwatch using tkinter in python
Things to keep in mind when using cgi with python.
A story about trying to implement a private variable in Python.
I want to make input () a nice complement in python
I wrote python3.4 in .envrc with direnv and allowed it, but I got a syntax error
I made a class to get the analysis result by MeCab in ndarray with python