Python Glossary Русский

all Python 3osterm

os.sep

constants

The path component separator on this system: '/' on Unix and macOS, '\\' on Windows.

Syntax

os.sep  # '/' на Unix, '\\' на Windows

There is no need to glue paths with os.sep by hand: os.path.join and pathlib do it and handle the edge cases correctly. Windows also understands '/', so it has os.altsep = '/', while on Unix that is None. os.sep is mostly useful for taking existing paths apart.

Examples

import os
print(repr(os.sep))  # → ? '/' на Unix, '\\' на Windows
print('home/user/file.txt'.replace('/', os.sep).split(os.sep))  # → ['home', 'user', 'file.txt']
print(os.path.join('home', 'user'))  # → ? home/user — так собирают пути

See also

Open in the full glossarydocs.python.orgReport an inaccuracy