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!
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.
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.
__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.
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.
You should take a look at: os.path.relpath
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With