Python Glossary Русский

all Python 3osfunction

os.execv

os — running programs (exec)

Replace the current process with a new program (path + args list).

Syntax

os.execv(...)

The first list item must be the program name: execv('/bin/ls', ['ls', '-l']). Pass ['-l'] and the program thinks its name is '-l' and receives no arguments; an empty list raises ValueError. The current process is replaced entirely — code after the call never runs and unflushed buffers are lost, so call sys.stdout.flush() first.

Examples

import os
args = ['ls', '-l', '/tmp']   # args[0] — имя программы, каким её увидит сама программа
print(args[0], len(args))   # → ls 3
print(os.execv('ls', args))   # → FileNotFoundError: execv не ищет в PATH, нужен полный путь
import os
# os.execv('/bin/ls', args)   # только Unix: текущий процесс заменяется на ls, строки ниже уже не выполнятся

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy