子进程调用不起作用

时间:2015-09-13 14:27:27

标签: python raspberry-pi

我这里有这段代码

pifm.py

#!/usr/bin/python

from subprocess import call

def play_old( filename ):
   call(["./pifm ", filename, "88.0", "88307"])
   return

def play_message():
    call(["./WelcomeMessage"])
    return 

Run.py

#Import
import pifm
import glob, os
import random

#Setup Vars
songs = [""]
randomNum = 0

#Find Music in directory
os.chdir("Music")
for file in glob.glob("*.wav"):
    songs.append(file)

os.chdir("..")
while True:
    randomNum = random.randint(1,len(songs)-1)
    print("Playing Song: " + str(songs[randomNum]))
    pifm.play(str(songs[randomNum]))
    pifm.play_message()

每次我运行它时都会抛出这个错误而我无法找出原因

Traceback (most recent call last):
  File "run.py", line 19, in <module>
    pifm.play(str(songs[randomNum]))
  File "/home/pi/PyRate-Radio/pifm.py", line 6, in play
    call(["./pifm ", filename, "88.0", "88307"])
  File "/usr/lib/python2.7/subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

任何想法?

1 个答案:

答案 0 :(得分:0)

您只传递了wav文件的文件名,而不包括Music目录名 还要注意对call语句的更改,文件名是一组双引号来处理带空格的文件名,并且(显然)确保2个脚本是可执行的。

pifm.py

#!/usr/bin/python
from subprocess import call

def play( filename ):
   call("./pifm "+'"'+filename+'"'+" 88.0"+" 88307", shell=True)
   return

def play_message():
    call("./WelcomeMessage")
    return     

run.py

import pifm
import glob, os
import random

#Setup Vars
songs = [""]
randomNum = 0

#Find Music in directory
for file in glob.glob("Music/*.wav"):
    songs.append(file)

while True:
    randomNum = random.randint(1,len(songs)-1)
    print("Playing Song: " + str(songs[randomNum]))
    pifm.play(str(songs[randomNum]))
    pifm.play_message()