1057

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:

"C:/example/cwd/mydir/myfile.txt"
0

12 Answers 12

1519
>>> 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'
8
  • 43
    Note: On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)). So if mydir/myfile.txt do not under os.getcwd(), the absolute path is not the real path.
    – coanor
    Commented Nov 25, 2014 at 4:15
  • 41
    @coanor ? Without an explicit root, mydir/myfile.txt implicitly refers to a path inside the current working directory as is therefore equivalent to ./mydir/myfile.txt. That might not be the path you intended to input, but it seems like the correct interpretation of the path as far as I can tell. Could you elaborate?
    – jpmc26
    Commented Jan 8, 2015 at 22:46
  • 3
    @jpmc26 I don't exactly follow coanor, but I would say that (contrary to what I presumed), there is no linkage between the argument to the abspath function and a real file. You could give any pathname- non-existent files and directory heirarchies are fine- and abspath will simply resolve the bits of the path (including the parent directory ".." element) and return a string. This is just a string computed from the current directory; any correlation to an actual file is accidental, it seems. Try os.path.abspath("/wow/junk/../blha/hooey"). It works.
    – Mike S
    Commented Sep 12, 2018 at 2:01
  • 2
    @MikeS I'm honestly not sure why that would be unexpected behavior. It's absolute path, not absolute file or directory. If you want an existence check, call os.path.exists. To the contrary, systems like PowerShell that insist on the path existing with the standard path resolution function are a pain to use.
    – jpmc26
    Commented Sep 12, 2018 at 3:28
  • 1
    @jpmc26 To assume that a path is just a string that looks like a pathname is not clear at all, and goes counter to how I've been thinking and speaking of pathnames for many years. I quote the Python 3 docs for abspath: "Return a normalized absolutized version of the pathname path." Not a"...version of the string path". A pathname, as defined by Posix, is "A string that is used to identify a file." The Python docs are explicit about relpath: "the filesystem is not accessed to confirm the existence or nature of path". If the argument here is obvious, why be explicit for relpath?
    – Mike S
    Commented Sep 17, 2018 at 18:07
136

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

5
  • 6
    Very helpful. Using os.path.abspath() gave me an error: AttributeError: 'NoneType' object has no attribute 'startswith', using Path().resolve() does not with the same relative filepath. (Linux and Python3.4) Commented Aug 31, 2015 at 16:14
  • 3
    According to my experiment, in window platform, resolve() returns full path to you only if it is able to resolve() file. But, os.path.abspath returns full path to you anyway even the file does not exists. However, in linux, it always return absolute path
    – Mond Wan
    Commented Jul 31, 2020 at 9:04
  • Why is that when the Path(__file__) alone (without the resolve method) is used in a module being imported along with a package, gives the absolute path instead of the relative path?
    – aderchox
    Commented Aug 8, 2020 at 8:50
  • 1
    Note that resolve() will follow symlinks. If you don't want this, use absolute() instead, which will leave not resolve symlinks. Commented Aug 18, 2021 at 16:09
  • 1
    If one wants to stay with the legacy os.path library, one can also use os.path.realpath(PATH) to get the same functionality as pathlib.Path(PATH).resolve(). Especially, realpath() also follows symlinks.
    – Kai Petzke
    Commented Mar 24 at 0:40
40
import os
os.path.abspath(os.path.expanduser(os.path.expandvars(PathNameString)))

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

1
  • 2
    I know this is a rather old answer, but isn't there one command that does all this in one call? Seems like this would be what would make the incoming path the most flexible and hence often needed (at least in my case that's true). Commented Oct 5, 2021 at 10:39
32

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
30

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()
'C:\\example\\cwd\\mydir\\myfile.txt'
5
  • 3
    Too bad they never got a proper filename abstraction module into the stdlib. Commented Sep 26, 2008 at 12:08
  • 1
    @Torsten Marek: it's a sore and longstanding omission.
    – flow
    Commented Feb 11, 2011 at 23:58
  • 7
    They did now for Python 3.4: pathlib. See my answer in this thread. Commented Oct 24, 2014 at 1:20
  • 1
    There are yypos in this answer. It should be from path import Path then Path('mydir/myfile.txt').abspath()
    – Frak
    Commented Jun 5, 2017 at 14:51
  • 1
    There are no typos, you may have been using a different path module. The linked module uses a class named path.
    – Tom
    Commented Jun 6, 2017 at 23:42
18

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

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

3
  • 2
    This is not what was being asked. They asked about a path in relation to the current working directory, which is not the same thing as the script directory, though they may sometimes end up having the same value. Commented May 23, 2019 at 4:16
  • this will fail if the script is called from somewhere other than it's current location, for example from the parent folder. Commented Jul 4, 2022 at 17:30
  • @MarcCompere no, it won't. The value of filename would be different, but once you use os.path.abspath() you will get the absolute path for that file regardless of where you calling it from. Commented Jul 27, 2022 at 9:01
15

Today you can also use the unipath package which was based on path.py: http://sluggo.scrapping.cc/python/unipath/

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

13

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

from pathlib import Path

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

print(fpath)
1
  • 1
    This answer was given with more detail already by Mad Physicist in 2018. Commented Sep 22, 2021 at 11:45
2

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'

1

if you are on a mac

import os
upload_folder = os.path.abspath("static/img/users")

this will give you a full path:

print(upload_folder)

will show the following path:

>>>/Users/myUsername/PycharmProjects/OBS/static/img/user
1
  • 7
    Identical to the accepted answer, except it arrived 10 years late.
    – wim
    Commented Oct 4, 2020 at 23:29
0

Might be useful to see the differences between pathlib.Path.resolve and pathlib.Path.absolute :

The code is being run at:

>>> import os; os.getcwd()
'/home/mat'

We are using pathlib.Path:

>>> from pathlib import Path

No difference when it's a simple path with normal file:

>>> rel = Path("file")
>>> rel.resolve()
PosixPath('/home/mat/file')
>>> rel.absolute()
PosixPath('/home/mat/file')

But a path with ".." only resolve gives you the "real" path:

>>> rel = Path("file")
>>> rel.absolute()
PosixPath('/home/mat/../file')
>>> rel.resolve()
PosixPath('/home/file')

However, if a Path points to a symlink, resolve will point to the real file...

>>> rel = Path("symboliclink")
>>> rel.absolute()
PosixPath('/home/mat/symboliclink')
>>> rel.resolve()
PosixPath('/tmp/link.test')

Choose wisely.

-1

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
1
  • there's no need for a specific platform solution to find the path of an absolute file. I think sticking with Path module is a better idea.
    – Gino_P
    Commented Feb 5 at 15:00

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