Python Path.absolute
Convert a relative name into an absolute pathname.
A simple example
Path.absolute
- Start with Current folder /work, relative file a
- Do this Make the path absolute
- Result /work/a
Syntax
receipt.absolute()
Run these examples in Python
Save one example at a time as lesson.py. Use Python 3.9 or later.
python3 lesson.pyGive an attachment an absolute location
A clerk chooses a receipt by a short local name. A desktop helper working in another directory needs an absolute pathname to display the intended attachment without guessing the original working directory.
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as folder:
base = Path(folder)
receipt = Path("receipt.txt").absolute()
if receipt.is_absolute() and receipt.name == "receipt.txt":
print("absolute")
absolute
The result has a root and keeps the receipt filename.
Locate a rehearsal score
The choir stores each score inside a season folder. Resolve the child name against the absolute working folder. What final filename can the desk recover from the absolute result?
Locate a workshop handout
An instructor stores the current template in a class folder. Resolve the child name against the absolute working folder. What final filename can the desk recover from the absolute result?
What to watch for
An absolute pathname alone does not verify existence or resolve symbolic links.