os.path.abspath

abspath(p) is normpath(join(os.getcwd(), p)) — the result depends on the folder you run from, and the file does not have to exist. realpath asks the file system and follows symlinks, so it gives the true location; with strict=True (3.10+) it raises if a component is missing.

os.path functionPython 3.0+ (realpath strict 3.10+)Live demo
Common call
os.path.abspath(__file__)
Returns
'/home/ada/project/app.py'
Replaces
os.getcwd() + "/" + path
Watch out
Relative input depends on the current directory, which may not be the script's folder
os.path.abspath(path) / os.path.realpath(path, *, strictstrict — realpath only (3.10+): True raises OSError when a path component does not exist or a symlink loop is found.type: bool · default: False=False)
→ str | bytes

Demo

Live evaluation
abspath needs the current directory; here a fixed one stands in for os.getcwd(), so the output is the same everywhere.
Try:
Inputs
pathstrrelative or absolute POSIX path
Code
import posixpath
cwd = '/home/ada/project'          # stands in for os.getcwd()
posixpath.abspath(posixpath.join(cwd, 'data/raw.csv'))
Result
'/home/ada/project/data/raw.csv'

A relative path is glued onto the current directory and normalized: '../shared/./lib' from /home/ada/project is /home/ada/shared/lib. An absolute path ignores the current directory (join discards it) and is only normalized. On your machine os.path.abspath uses your real current directory — and on Windows ntpath.abspath asks Windows for the full path, drive letter included.

Parameters

NameTypeRequiredDescription
pathstr | bytes | PathLikeyesRelative paths are resolved against os.getcwd().
strictboolno (False)realpath only (3.10+): True raises OSError when a path component does not exist or a symlink loop is found.

Return value

str | bytes — An absolute, normalized path. realpath additionally has no symlinks in it.

Common patterns

Files next to the script, from any working directory
Anchor on __file__, never on the cwd.
import os
BASE = os.path.dirname(os.path.abspath(__file__))
templates = os.path.join(BASE, 'templates')
Canonical path for comparisons and caches
realpath resolves symlinks so two spellings of one file compare equal.
import os
key = os.path.normcase(os.path.realpath(path))
Fail on missing components
strict=True raises instead of guessing.
import os
resolved = os.path.realpath(path, strict=True)

Examples

1. Absolute input: just normalized
import posixpath posixpath.abspath('/usr/local/../bin')
Returns
'/usr/bin'
2. Relative input uses the cwd
import os os.path.abspath('data.txt') == os.path.join(os.getcwd(), 'data.txt')
Returns
True
3. abspath('.') is the cwd
import os os.path.abspath('.') == os.getcwd()
Returns
True
4. The result is always absolute
import os os.path.isabs(os.path.abspath('x'))
Returns
True
5. The file need not exist
import os os.path.exists(os.path.abspath('nope.txt'))
Returns
False
6. Without symlinks, realpath = abspath
import os os.makedirs('real') os.path.realpath('real') == os.path.abspath('real')
Returns
True
7. strict=True raises for missing paths
import os try: os.path.realpath('nope.txt', strict=True) except OSError as e: result = type(e).__name__ result
Returns
'FileNotFoundError'

Pitfalls

1. Resolving data files against the current directory
Relative paths follow os.getcwd(), which is wherever the program was started — not where the script lives.
abspath('config.toml')
import os, contextlib
os.mkdir('elsewhere')
with contextlib.chdir('elsewhere'):
    p = os.path.abspath('config.toml')
os.path.basename(os.path.dirname(p))
'elsewhere'
anchor on a fixed folder
import os, contextlib
os.mkdir('elsewhere')
base = os.path.abspath('.')
with contextlib.chdir('elsewhere'):
    p = os.path.join(base, 'config.toml')
os.path.dirname(p) == base
True
2. Expecting abspath to check the file
abspath and (non-strict) realpath are happy with paths that do not exist.
abspath
import os
os.path.isabs(os.path.abspath('typo.txt'))
True
realpath(strict=True)
import os
try:
    os.path.realpath('typo.txt', strict=True)
    found = True
except FileNotFoundError:
    found = False
found
False

When to use

Use it
  • Turning user input or __file__ into a full path (abspath)
  • Finding the real location behind symlinks (realpath)
Reach for something else
  • pathlib code → Path.absolute() / Path.resolve()
  • Just cleaning text → normpath

Notes

CPython impl
posixpath.abspath: if not isabs(path): path = join(os.getcwd(), path); return normpath(path). ntpath.abspath asks Windows for the full path (nt._getfullpathname)
realpath
POSIX: walks the path component by component with lstat/readlink, detecting loops. Windows: also turns 8.3 short names such as C:\PROGRA~1 into long ones, and returns the case the OS reports (drive letter capitalized)
strict
Added in 3.10. Without it, the path is resolved up to the first missing or failing component and the rest is appended unchanged. 3.13.4 added a third choice, strict=os.path.ALLOW_MISSING: only FileNotFoundError is tolerated

FAQ

os.path.abspath(path), or Path(path).resolve() with pathlib (which also follows symlinks, like realpath).