Redirect stderr in stdout to exec-ed process from python?

In a bash script, I can write:

exec 2>&1
exec someprog

And stderr output someprogwill be redirected to stdout.

Is there a way to do a similar thing using python os.exec*functions?

It does not have to be portable, just work on Linux.

+3
source share
1 answer

os.dup2(1, 2)

Illustrative examples

Let it be executed /bin/lswith a dummy argument so that it complains about stderr.

$ python -c "import os; os.execl ('/ bin / ls', '', 'ffweew')" 1> / dev / null
: ffweew: No such file or directory
$ python -c "import os; os.execl('/bin/ls', '', 'ffweew')" 2>/dev/null
$ python -c "import os; os.dup2(1, 2); os.execl('/bin/ls', '', 'ffweew')" 1>/dev/null
$ python -c "import os; os.dup2(1, 2); os.execl('/bin/ls', '', 'ffweew')" 2>/dev/null
: ffweew: No such file or directory
$ 

, ls stdout stderr.

3- 4- Python 1 2, .

+4

Source: https://habr.com/ru/post/1733854/


All Articles