.resolve()
?.resolve()
is a method of the Path
object in the pathlib
module.
It returns the absolute path of the file or directory.
It resolves symbolic links, .
(current directory), and ..
(parent directory) into a clean, canonical path.
.resolve()
from pathlib import Path
p = Path("myfolder/../project/settings.py")
print(p)
Output:
myfolder/../project/settings.py
👉 It still contains ..
(not fully clean/absolute).
.resolve()
from pathlib import Path
p = Path("myfolder/../project/settings.py").resolve()
print(p)
Output (absolute path):
C:\Users\Abhi\Desktop\project\settings.py # Windows
/home/abhi/project/settings.py # Linux/Mac
👉 Now you get the full, exact location of the file.
If a file path points to a symlink, .resolve()
follows it to the real file.
p = Path("shortcut_to_file").resolve()
print(p)
If shortcut_to_file
→ /home/abhi/real_data.txt
Then .resolve()
returns:
/home/abhi/real_data.txt
.resolve()
used in Django’s BASE_DIR
?In settings.py
:
BASE_DIR = Path(__file__).resolve().parent.parent
__file__
→ "settings.py"
.resolve()
→ gives the absolute path (no matter where you run the project from).
.parent.parent
→ climbs up two directories → project root.
👉 This ensures Django always knows the correct root directory regardless of:
whether you run with python manage.py runserver
or from another folder using imports.
.resolve()
Absolute Path Guarantee → You always know where the file really is.
Cross-Platform Consistency → Works on Windows, Mac, Linux.
Removes Ambiguity → Clears .
and ..
from paths.
Follows Symlinks → Ensures you get the real file.