Why python3 can't execute some linux command?

Viewed 147

I can execute mjpg-streamer using raspberry-pi 3 terminal.

And Below is command that I use.

mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 800x448" -o "output_http.so -p 8090 -w /usr/local/share/mjpg-streamer/www/"

And now I want to execute it on python 3. So I try to do it using os.system() and subprocess.call() but It failed to execute it and webcam go wrong after running code so I have to reboot raspberry-pi 3. even os.system() works well when code is like os.system('python3 test.py').

Is it not possible to execute mjpg-streamer using pathon 3 code?

below is my code.

import os

os.system('mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 800x448" -o "output_http.so -p 8090 -w /usr/local/share/mjpg-streamer/www/"')
1 Answers

you can try use subprocess that allows to save stdout and stderr too:

    import subprocess
    ### define the command
    command = 'mjpg_streamer -i "input_uvc.so -d /dev/video0 -r 800x448" -o "output_http.so -p 8090 -w /usr/local/share/mjpg-streamer/www/"'
    ### execute the command and save stdout and stderr as variables
    output, error = subprocess.Popen(command, universal_newlines=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

and you will have stdout saved in "output" and "stderr" in "error" variable.

BTW: it would be advisable to use the listed format

Related