gpt4 book ai didi

python - 为什么python3不能执行一些linux命令?

转载 作者:行者123 更新时间:2023-12-03 23:11:30 24 4
gpt4 key购买 nike

我可以使用 raspberry-pi 3 终端执行 mjpg-streamer。

下面是我使用的命令。

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

现在我想在 python 3 上执行它。所以我尝试使用 os.system() 来执行它。和 subprocess.call() 但它无法执行它并且运行代码后网络摄像头出错,所以我必须重新启动 raspberry-pi 3。甚至 os.system()当代码类似于 os.system('python3 test.py') 时效果很好.

是否无法使用 pathon 3 代码执行 mjpg-streamer?

下面是我的代码。

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/"')

最佳答案

您也可以尝试使用允许保存 stdout 和 stderr 的子进程:

    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()

并且您将标准输出保存在“输出”和“错误”变量中的“标准错误”中。

顺便说一句:建议使用 listed format

关于python - 为什么python3不能执行一些linux命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56632659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com