all Python 3osfunction
os.system
os — abnormal termination
Run a command in the system shell and wait for it. Returns the encoded wait status on Unix and the exit code on Windows.
Syntax
os.system(command)
On Unix it returns an encoded wait status rather than the exit code: os.system('exit 3') gives 768, and os.waitstatus_to_exitcode converts it. On Windows the result is the code itself. The command goes through a shell, so a string containing user data invites command injection. Output is not captured. New code should use subprocess.run with an argument list.
Examples
# Python 3.9+
import os
print(os.system('echo привет')) # → сначала оболочка печатает «привет», потом 0 — код успешного завершения
code = os.system('exit 3')
print(code) # → зависит от ОС: 3 в Windows, 768 (то есть 3 << 8) в Unix
print(os.waitstatus_to_exitcode(code)) # → только Unix: 3 — статус ожидания распакован в настоящий код возврата
print(os.system('такой-команды-нет')) # → оболочка печатает своё сообщение об ошибке, код ≠ 0; исключение НЕ бросается
See also
Open in the full glossarydocs.python.orgReport an inaccuracy