Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve relative paths in python?

Tags:

python

path

I have Directory structure like this

projectfolder/fold1/fold2/fold3/script.py 

now I'm giving script.py a path as commandline argument of a file which is there in

fold1/fold_temp/myfile.txt 

So basically I want to be able to give path in this way

../../fold_temp/myfile.txt   >>python somepath/pythonfile.py -input ../../fold_temp/myfile.txt 

Here problem is that I might be given full path or relative path so I should be able to decide and based on that I should be able to create absolute path.

I already have knowledge of functions related to path.

Question 1

Question 2

Reference questions are giving partial answer but I don't know how to build full path using the functions provided in them.

like image 286
niyant Avatar asked Sep 29 '15 08:09

niyant


People also ask

How does Python handle relative paths?

relpath() method in Python is used to get a relative filepath to the given path either from the current working directory or from the given directory. Note: This method only computes the relative path. The existence of the given path or directory is not checked.

How do you change a relative path to an absolute path in Python?

Use abspath() to Get the Absolute Path in Python abspath() with the given path to get the absolute path. The output of the abspath() function will return a string value of the absolute path relative to the current working directory.

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.


2 Answers

try os.path.abspath, it should do what you want ;)

Basically it converts any given path to an absolute path you can work with, so you do not need to distinguish between relative and absolute paths, just normalize any of them with this function.

Example:

from os.path import abspath filename = abspath('../../fold_temp/myfile.txt') print(filename) 

It will output the absolute path to your file.

EDIT:

If you are using Python 3.4 or newer you may also use the resolve() method of pathlib.Path. Be aware that this will return a Path object and not a string. If you need a string you can still use str() to convert it to a string.

Example:

from pathlib import Path filename = Path('../../fold_temp/myfile.txt').resolve() print(filename) 
like image 68
Salo Avatar answered Sep 27 '22 21:09

Salo


For Python3, you can use pathlib's resolve functionality to resolve symlinks and .. components.

You need to have a Path object however it is very simple to do convert between str and Path.

I recommend for anyone using Python3 to drop os.path and its messy long function names and stick to pathlib Path objects.

like image 36
Bryce Guinta Avatar answered Sep 27 '22 22:09

Bryce Guinta