Python Glossary Русский

Python 3.2+Linux · macOSosfunction

os.getenvb

os — environment

Like getenv, but the key and value are bytes; Unix only. Availability: Unix.

Syntax

os.getenvb(...)

Use it when a variable's value does not decode in the filesystem encoding, e.g. a path in a foreign encoding. It works on os.environb and exists only where os.supports_bytes_environ is true — not on Windows. In ordinary code os.getenv is enough: on Unix undecodable bytes survive as surrogates.

Examples

import os
print(os.supports_bytes_environ)   # → ? True на Unix (там есть getenvb), False на Windows
os.environb[b'GLOSSARY_DEMO'] = b'42'   # → ? только Unix: кладём переменную в байтовое окружение
print(os.getenvb(b'GLOSSARY_DEMO'))   # → b'42' — и ключ, и значение остаются bytes
print(os.getenvb(b'GLOSSARY_NO_SUCH', b'fallback'))   # → b'fallback' — второй аргумент это значение по умолчанию
print(os.getenvb('GLOSSARY_DEMO'))   # → TypeError — str-ключ не подходит, для строк есть os.getenv

See also

Open in the full glossarydocs.python.orgReport an inaccuracy