Python Glossary Русский

all Python 3osfunction

os.path.normpath

os.path — path parsing

Collapse redundant separators and up-level references (`.`/`..`).

Syntax

os.path.normpath(path)

It works on the string without looking at the disk, so it may change a path's meaning: if 'a' is a symlink, 'a/../b' on disk and the normalised 'b' are different places. For the real on-disk path use realpath. An empty string becomes '.'. Two leading slashes are kept on POSIX ('//a'), while three or more collapse to one.

Examples

import os
print(os.path.normpath('a/./'))   # → a
print(os.path.normpath('a/b/..'))   # → a
print(os.path.normpath('a/../нет-такого/../b'))   # → b
print(os.path.normpath(''))   # → .
print(os.path.normpath('link/..'))   # → . — схлопывание чисто текстовое: если link был симлинком, настоящего родителя даст только os.path.realpath

See also

Open in the full glossarydocs.python.orgReport an inaccuracy