APIs Reference

Resolving Paths with pathlib.Path.resolve()

resolve() makes a path absolute, collapses dot segments, expands symbolic links, and applies on-disk case on Windows. With the default strict=False the path need not exist, but resolution still reads the filesystem and can raise OSError.

The short answer

Path.resolve() returns a new Path that is absolute, has . and .. segments collapsed, and has every symbolic link replaced by its target. On Windows it also normalizes the drive letter and applies the case recorded on disk for parts that exist.

The default is strict=False since Python 3.6: the path does not have to exist, and any unresolved remainder is appended unchanged. strict=True requires the whole path to exist and raises FileNotFoundError otherwise. Even with strict=False, resolution is not a pure string operation: it reads the current working directory and queries the filesystem for symlinks and canonical names, so it can raise OSError on permission errors, unreachable mounts, or symlink loops.

A minimal example

from pathlib import Path

p = Path("data") / ".." / "data" / "report.csv"
print(p)            # data/../data/report.csv
print(p.resolve())  # /home/user/project/data/report.csv

With symlinks:

from pathlib import Path

current = Path("/srv/app/current")  # symlink to /srv/app/releases/2026-11-29
print(current.resolve())            # /srv/app/releases/2026-11-29

And with strict mode:

from pathlib import Path

Path("missing.txt").resolve(strict=True)
# FileNotFoundError: [Errno 2] No such file or directory: ...

What resolve() changes

Input Typical result Note
a/./b /cwd/a/b dot segment removed
a/../b /cwd/b .. applied after symlinks
link target path all links followed
missing/../x /cwd/x remainder kept when strict=False
~/notes /cwd/~/notes no tilde expansion
C:\TEMP\..\x C:\x, canonical case Windows normalization

The .. behavior is the subtle one. Resolution follows POSIX realpath semantics: symlinks are expanded first, then .. applies to the resolved location. If link points to /srv/app/releases/2026-11-29, then Path("link/..").resolve() is /srv/app/releases, not the directory containing link.

What it requires from the filesystem

  • The current working directory, for a relative path. If it has been deleted, resolve() can fail.
  • Search permission on every directory it traverses. Denied access raises PermissionError, a subclass of OSError.
  • Readable symlinks. A dangling link is handled when strict=False (the target path is returned as far as it can be resolved), but a loop cannot be resolved. Recent Python versions raise OSError for loops; older ones raised RuntimeError.
  • Access to Windows volumes and shares when the path names one, which can fail for disconnected drives.

None of this means the final file exists. resolve() is about naming, not existence. A resolved path is only as good as the working directory it was resolved against: running the same code from a different directory produces a different absolute path, which is a common source of ENOENT errors on relative paths.

Choosing between resolve, absolute, and realpath

  • Path.absolute() does not expand symlinks. Use it when you only want an absolute spelling and do not need the target.
  • os.path.normpath() is purely lexical; it collapses .. without consulting links, so it can disagree with resolve().
  • os.path.realpath(path, strict=False) performs the same filesystem-aware resolution and returns a string. Use it when you do not need a Path object.
  • Path.expanduser() handles ~; resolve() does not.

Practical checks

  • Do not call resolve(strict=True) on an output file you are about to create; it will raise before you write anything.
  • Check resolved.exists() separately when existence matters.
  • Prefer resolve() before comparing paths for equality, but remember two hard links to the same file still resolve to different names unless you compare inodes.
  • Catch OSError around resolution when the input comes from configuration or user input.

Sources

Revision history

Published
Updated
— Initial publication.