Python Glossary Русский

all Python 3osfunction

os.symlink

os — links

Create a symbolic link to a path. On Windows it needs administrator rights or developer mode.

Syntax

os.symlink(...)

Argument order matches ln -s: target first, then the link name. A relative target is resolved from the link's directory, not from the current directory — a common mistake. The target is checked on use, not at creation, so a broken link is created without error. On Windows you need admin rights or developer mode, and target_is_directory=True for a directory.

Examples

import os
os.symlink('data.txt', 'data-link.txt')   # → ? только Unix (на Windows нужны права разработчика): создаёт символическую ссылку
print(os.path.islink('data-link.txt'))   # → True
print(os.readlink('data-link.txt'))   # → ? data.txt
os.symlink('logs', 'logs-link', target_is_directory=True)   # → ? ссылка на каталог; флаг учитывается только на Windows
os.symlink('data.txt', 'data-link.txt')   # → FileExistsError — существующий dst молча не перезаписывается
os.remove('data-link.txt')   # → ? удаляет саму ссылку, файл data.txt остаётся на месте

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy