I have Directory structure like this
now I'm giving script.py a path as commandline argument of a file which is there in
So basically I want to be able to give path in this way
>>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.
Reference questions are giving partial answer but I don't know how to build full path using the functions provided in them.
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.
from os.path import abspath
filename = abspath('../../fold_temp/myfile.txt')
It will output the absolute path to your file.
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.
from pathlib import Path
filename = Path('../../fold_temp/myfile.txt').resolve()