Skip to main content

How to Get the Absolute Path of a File in Python with Path.resolve()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A path like data only makes sense next to the folder the program runs in. When you print it in a message or store it for later, the full path is clearer: it says exactly where the file is. Path.resolve() turns a relative path into that absolute one. This guide resolves three paths on Python 3.15.2.

Syntax​

Syntax
Path("<path>").resolve(strict=<True or False>)
PartWhat it means
Path("<path>")A relative or absolute path
.resolve()Returns the absolute path, with .. parts removed and symbolic links followed
strict=<True or False>Optional. With True, a path that doesn't exist raises an error. The default is False

The pathlib documentation says: "Make the path absolute, resolving any symlinks. A new path object is returned", and "'..' components are also eliminated".

The Example​

The script prints a relative path, the same path resolved, and a path with .. in it, resolved. It runs in a project folder called python-basics.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

absolute.py
from pathlib import Path

relative = Path("data")
print(relative)
print(relative.resolve())
print(Path("data/../notes.txt").resolve())

The absolute.py script in a macOS-style code window: it prints Path("data"), the same path's resolve(), and Path("data/../notes.txt").resolve().

Step 2: Run It​

Terminal
uv run python absolute.py
Output
data
/Users/you/source/python-basics/data
/Users/you/source/python-basics/notes.txt

The terminal after uv run python absolute.py: data, then /Users/you/source/python-basics/data, then /Users/you/source/python-basics/notes.txt.

resolve() put the folder the script ran in in front of data. In the last line, data/.. went into data and back out again, so resolve() removed both parts and left notes.txt in the project folder. Neither data nor notes.txt exists in this project: by default, resolve() doesn't check. The command works unchanged in PowerShell. On Windows, the path starts with a drive letter, such as C:\.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting ~ to mean your home folderPath("~/data").resolve() gave /Users/you/source/python-basics/~/data: ~ is just a folder name to PythonCall .expanduser() first: Path("~/data").expanduser() gave /Users/you/data
Expecting resolve() to check that the path existsIt returned paths to data and notes.txt, which don't existPass strict=True to get FileNotFoundError instead, or check with .exists()

FAQ​

What's the difference between absolute() and resolve()?​

absolute() only adds the current folder in front. The documentation: "Make the path absolute, without normalization or resolving symlinks." For Path("data/../notes.txt"), absolute() gave /Users/you/source/python-basics/data/../notes.txt, and resolve() gave /Users/you/source/python-basics/notes.txt.

What does strict=True do?​

It makes resolve() raise an error when the path doesn't exist: Path("data").resolve(strict=True) stopped with FileNotFoundError: [Errno 2] No such file or directory: '/Users/you/source/python-basics/data'.

Conclusion​

Path("data").resolve() turned a relative path into /Users/you/source/python-basics/data, and removed the .. from data/../notes.txt. It doesn't expand ~ and doesn't check that the path exists, unless you ask with expanduser() and strict=True.

Additional Resources​