我需要通过python代码在树莓派中执行以下两个shell命令:
cd Desktop\fm_transmitter-master
sudo python ./PiStation.py -f 96 example.mp3
但是我不知道该怎么做,有人可以帮我吗?
import subprocess
subprocess.call('cd Desktop\fm_transmitter-master', shell=True)
subprocess.call('sudo python ./PiStation.py -f 96 syria.mp3', shell=True)
执行命令
答案 0 :(得分:0)
import os
desktop = os.path.expanduser("~/Desktop")
script_path = os.path.join(desktop, 'fm_transmitter-master', 'PiStation.py')
song_path = os.path.join(desktop, 'fm_transmitter-master', 'syria.mp3')
subprocess.call('sudo python ' + script_path + ' -f 96 ' + song_path, shell=True)
答案 1 :(得分:0)
解决了它:
path = "/home/pi/Desktop/fm_transmitter-master/"
os.chdir(path)
args = ["sudo", "python", "PiStation.py", "-f", "96", "song.mp3"]
pi_station = subprocess.Popen(args, stdout=subprocess.PIPE)