Python Glossary Русский

all Python 3Linux · macOSosfunction

os.WEXITSTATUS

os — status decoding (W*)

Return the exit code of a normally-terminated child. Availability: Unix.

Syntax

os.WEXITSTATUS(...)

It is meaningful only after WIFEXITED is true: for a process killed by a signal it returns a meaningless number. It yields the low 8 bits of the code, so sys.exit(256) in the child becomes 0. os.waitstatus_to_exitcode is simpler and safer, as it tells all cases apart itself.

Examples

# Python 3.9+
import os
status = os.system('exit 7')  # → ? только Unix: статус в формате wait(), здесь 1792 (7 << 8)
print(os.WEXITSTATUS(status))  # → 7 — код выхода команды
print(os.WIFEXITED(status))  # → True — лишь при этом WEXITSTATUS осмыслен
killed = os.system('kill -9 $$')  # → ? только Unix: оболочка убивает сама себя сигналом SIGKILL
print(os.WIFEXITED(killed), os.WTERMSIG(killed))  # → ? только Unix: False 9 — здесь нужен WTERMSIG, WEXITSTATUS вернёт бессмыслицу
print(os.waitstatus_to_exitcode(status))  # → 7 — современная однострочная замена паре WIFEXITED + WEXITSTATUS

See also

Open in the full glossarydocs.python.orgReport an inaccuracy