38

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.

2

5 Answers 5

96

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)
1
  • 9
    provide a small example ... it is better than a link; otherwise, a good answer and would be up-voted...
    – IAbstract
    Feb 4, 2018 at 14:34
5

A practical example:

sys.argv[0] gives you the name of the current script

os.path.dirname() gives you the relative directory name

thus, the next line, gives you the absolute working directory of the current executing file.

cwd = os.path.abspath(os.path.dirname(sys.argv[0]))

Personally, I always use this instead of os.getcwd() since it gives me the script absolute path, independently of the directory from where the script was called.

1
  • 1
    Thank you, in 2022 <3 sys.argv[0] works great with runpy.run_path() Aug 19, 2022 at 4:05
4

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.

1
0
import os
dir = os.path.dirname(__file__)
path = raw_input()
if os.path.isabs(path):
    print "input path is absolute"
else:
    path = os.path.join(dir, path)
    print "absolute path is %s" % path

Use os.path.isabs to judge if input path is absolute or relative, if it is relative, then use os.path.join to convert it to absolute

5
  • 1
    This doesn't work as a solution as in dir path you will have a path for script file where as according to question I don't want to give that as a path..
    – niyant
    Sep 29, 2015 at 8:51
  • Similer solution is there in links provided in question... it is perfectly fine if your input file is in same directory or in some subdirectory having same parent
    – niyant
    Sep 29, 2015 at 8:53
  • os.path.join(dir, path) is already equal to path, if path is absolute (tested on Pyhon 3.6).
    – Mitar
    Oct 6, 2017 at 23:18
  • I am facing another situation, this method works for me, thank you
    – Lancer.Yan
    Aug 16, 2021 at 14:44
  • Thanks @Hooting! I have learnt something here when I came here to have the same question. This plus the answers above made my code more robust. So I upvote the answer here!
    – pedda
    Jan 15 at 2:03
0

In modern Python (3.4+), you can use pathlib, along with __file__, to resolve a relative path relative to the current script:

from pathlib import Path

p = Path(__file__).parent / "../../fold_temp/myfile.txt"

This is basically a one-liner. You don’t even need to resolve() the path, as you can directly work with the Path object like this:

with p.open() as f:
    ...

But yes, if you want to get the absolute path in string form, you can resolve it explicitly:

path = str(p.resolve())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.