Python Glossary Русский

all Python 3osfunction

os.path.abspath

os.path — making paths absolute

Returns a normalized absolute path (a relative one is resolved against the current directory).

Syntax

os.path.abspath(path)

It does not resolve symlinks — it is normpath(join(os.getcwd(), path)), so for a path that goes through a link and then '..' the result can point elsewhere. For the true on-disk path use realpath. The result depends on the current directory at call time: after os.chdir the same relative path yields a different answer.

Examples

import os
print(os.path.isabs(os.path.abspath('x')))   # → True
print(os.path.abspath('data/notes.txt').endswith(os.path.join('data', 'notes.txt')))   # → True (путь достроен от текущего каталога)
print(os.path.abspath('.') == os.getcwd())   # → True
print(os.path.abspath('a/b/../c') == os.path.join(os.getcwd(), 'a', 'c'))   # → True (abspath заодно нормализует '..')
print(os.path.exists(os.path.abspath('нет-такого-файла')))   # → False (abspath строит строку и не проверяет, существует ли путь)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy