简体   繁体   English

从python脚本(RaspberryPi)执行shell命令

[英]Execute shell command from python script (RaspberryPi)

I am trying to execute this command sudo mavproxy.py from a python script on raspberrypi. 我正在尝试从sudo mavproxy.py的python脚本执行此命令sudo mavproxy.py I can execute this in the shell and see it load. 我可以在外壳中执行此操作并查看其加载情况。

The code i have for my current test is: 我当前测试的代码是:

import subprocess
subprocess.call('sudo mavproxy.py')

running this the code executes however looking at the terminal window nothing happens. 运行此代码将执行,但是看着终端窗口什么也没有发生。 So i'm not sure whether it is executing correctly. 所以我不确定它是否正确执行。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Either pass the arguments as a list: 可以将参数作为列表传递:

subprocess.call(['sudo', 'mavproxy.py'])

Or use shell=True : 或者使用shell=True

subprocess.call('sudo mavproxy.py', shell=True)

The documentation is pretty clear about this. 文档对此非常清楚。 How did you learn to try it that way? 您是如何学会尝试的呢?

Import the call function from the subprocess module 从子流程模块导入调用函数

 from subprocess import call
 call('sudo mavproxy.py', shell=True)
import os

import os.path

os.system('sudo python /full/path/to/mavproxy.py')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM