How to get an absolute file path in Python


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:

Asked By: izb



>>> import os
>>> os.path.abspath("mydir/myfile.txt")

Also works if it is already an absolute path:

>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
Answered By: sherbang

Install a third-party path module (found on PyPI), it wraps all the os.path functions and other related functions into methods on an object that can be used wherever strings are used:

>>> from path import path
>>> path('mydir/myfile.txt').abspath()
Answered By: Tom

Today you can also use the unipath package which was based on

>>> from unipath import Path
>>> absolute_path = Path('mydir/myfile.txt').absolute()
>>> str(absolute_path)

I would recommend using this package as it offers a clean interface to common os.path utilities.

Answered By: user9903

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
>>> str(p)

Or on UNIX:

>>> from pathlib import Path
>>> p = Path("python3.4").resolve()
>>> p
>>> str(p)

Docs are here:

Answered By: twasbrillig

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:

Answered By: chikwapuro

In case someone is using python and linux and looking for full path to file:

>>> path=os.popen("readlink -f file").read()
>>> print path
Answered By: BND

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

If you only need a temporary string, keep in mind that you can use Path objects with all the relevant functions in os.path, including of course abspath:

from os.path import abspath

absolute = abspath(relative)  # absolute is a str object
Answered By: Mad Physicist

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 subprocess.

import sys,os

filename = sys.argv[0]

from there, you can get the script’s full path with:

>>> os.path.abspath(filename)

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+"/..")

For the parent path:

>>> os.path.abspath(filename+"/../..")

By combining "/.." with other filenames, you can access any file in the system.

Answered By: Lucas Azevedo
import os

Note that 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 $HOME).

Answered By: benjimin

You can use this to get absolute path of a specific file.

from pathlib import Path

fpath = Path('myfile.txt').absolute()

Answered By: Dhiaa Shalabi

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' )

That returns '/home/ecarroll/mydir/myfile.txt'

Answered By: Evan Carroll