On this page
os.readlink
Prev: os.read | Up: os | Next: os.remove
Rel: os.abort, os.access, os.chdir, os.chmod
Ref: docs.python.org — os.readlink
Tags: #public #python3 #stdlib #os
Import
import os
from os import readlink
Signature & Overview
os.readlink(path, *, dir_fd=None)
Return a string representing the path to which the symbolic link points.
Usage
import os
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
target = Path(tmp) / "real.txt"
target.write_text("ok", encoding="utf-8")
link = Path(tmp) / "alias.txt"
link.symlink_to(target)
assert Path(os.readlink(link)) == target
Gotchas
- Reads one symlink hop only: May return a relative path; use
os.path.realpath()orPath.resolve()to follow the full chain.
REPL Inspect
from os import readlink
help(readlink)
Linked from
Related notes
- tuts/python3/stdlib_module/os/read Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/remove Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/os/abort Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/access Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chdir Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chmod Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chown shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/close shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/cpu_count shared tags: #os, #stdlib