简体   繁体   中英

Execute shell command from python script (RaspberryPi)

I am trying to execute this command sudo mavproxy.py from a python script on raspberrypi. 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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