all Python 3osfunction
os.readlink
os — links
Return the path a symbolic link points to.
Syntax
os.readlink(...)
It returns the target exactly as stored in the link: a relative target stays relative — to the link's directory, not the current one. os.path.realpath gives the full path. A path that is not a link raises OSError. The target may not exist at all.
Examples
import os
open('data.txt', 'w').close() # цель ссылки — обычный файл
os.symlink('data.txt', 'link.txt') # → ? только Unix: создаёт ссылку link.txt на data.txt
print(os.readlink('link.txt')) # → ? data.txt — «сырая» цель ссылки, ровно как она записана
print(os.path.realpath('link.txt') == os.path.abspath('data.txt')) # → ? True — realpath разворачивает цель в абсолютный путь
print(os.path.islink('link.txt')) # → True — проверка перед вызовом readlink
print(os.readlink('data.txt')) # → OSError (EINVAL) — обычный файл не является символической ссылкой
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy