Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Compute relative path from one directory to another [duplicate]

Possible Duplicate:
How to calculate relative path between 2 directory path?

I need to compute the relative path from one directory to another (i.e. I set a path relative to the working directory, and want to compute the according relative path from any parent/subdir). What is a good way to do that in Python? I could not find much on the subject.

Thanks!

like image 729
moka Avatar asked Dec 22 '11 16:12

moka


People also ask

How do you pass a relative path in Python?

A relative path starts with / , ./ or ../ . To get a relative path in Python you first have to find the location of the working directory where the script or module is stored. Then from that location, you get the relative path to the file want.

What does resolve () in Python do?

The resolve method is used to convert a relative path to an absolute path. We can call it on the path object we previously created. The returned path is the absolute path of file1. Another way to convert a relative path to an absolute path is through the absolute method.

What is __ file __ in Python?

__file__ is a variable that contains the path to the module that is currently being imported. Python creates a __file__ variable for itself when it is about to import a module.

How do you get the full path of a relative path in Python?

To get an absolute path in Python you use the os. path. abspath library. Insert your file name and it will return the full path relative from the working directory including the file.


1 Answers

You should take a look at: os.path.relpath

like image 77
jcollado Avatar answered Sep 28 '22 04:09

jcollado