Given a path such as
"mydir/myfile.txt", how do I find the file’s absolute path in Python? E.g. on Windows, I might end up with:
>>> import os >>> os.path.abspath("mydir/myfile.txt") 'C:/example/cwd/mydir/myfile.txt'
Also works if it is already an absolute path:
>>> import os >>> os.path.abspath("C:/example/cwd/mydir/myfile.txt") 'C:/example/cwd/mydir/myfile.txt'
Today you can also use the
unipath package which was based on
>>> from unipath import Path >>> absolute_path = Path('mydir/myfile.txt').absolute() Path('C:\example\cwd\mydir\myfile.txt') >>> str(absolute_path) C:\example\cwd\mydir\myfile.txt >>>
I would recommend using this package as it offers a clean interface to common os.path utilities.
You could use the new Python 3.4 library
pathlib. (You can also get it for Python 2.6 or 2.7 using
pip install pathlib.) The authors wrote: “The aim of this library is to provide a simple hierarchy of classes to handle filesystem paths and the common operations users do over them.”
To get an absolute path in Windows:
>>> from pathlib import Path >>> p = Path("pythonw.exe").resolve() >>> p WindowsPath('C:/Python27/pythonw.exe') >>> str(p) 'C:\Python27\pythonw.exe'
Or on UNIX:
>>> from pathlib import Path >>> p = Path("python3.4").resolve() >>> p PosixPath('/opt/python3/bin/python3.4') >>> str(p) '/opt/python3/bin/python3.4'
Docs are here: https://docs.python.org/3/library/pathlib.html
if you are on a mac
import os upload_folder = os.path.abspath("static/img/users")
this will give you a full path:
will show the following path:
In case someone is using python and linux and looking for full path to file:
>>> path=os.popen("readlink -f file").read() >>> print path abs/path/to/file
Update for Python 3.4+
pathlib that actually answers the question:
from pathlib import Path relative = Path("mydir/myfile.txt") absolute = relative.absolute() # absolute is a Path object
from os.path import abspath absolute = abspath(relative) # absolute is a str object
This always gets the right filename of the current script, even when it is called from within another script. It is especially useful when using
import sys,os filename = sys.argv
from there, you can get the script’s full path with:
>>> os.path.abspath(filename) '/foo/bar/script.py'
It also makes easier to navigate folders by just appending
/.. as many times as you want to go ‘up’ in the directories’ hierarchy.
To get the cwd:
>>> os.path.abspath(filename+"/..") '/foo/bar'
For the parent path:
>>> os.path.abspath(filename+"/../..") '/foo'
"/.." with other filenames, you can access any file in the system.
import os os.path.abspath(os.path.expanduser(os.path.expandvars(PathNameString)))
expanduser is necessary (on Unix) in case the given expression for the file (or directory) name and location may contain a leading
~/(the tilde refers to the user’s home directory), and
expandvars takes care of any other environment variables (like
You can use this to get absolute path of a specific file.
from pathlib import Path fpath = Path('myfile.txt').absolute() print(fpath)
Given a path such as
mydir/myfile.txt, how do I find the file’s absolute path relative to the current working directory in Python?
I would do it like this,
import os.path os.path.join( os.getcwd(), 'mydir/myfile.txt' )