子进程调用不起作用 [英] subprocess call not working

查看:45
本文介绍了子进程调用不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有这个代码

#!/usr/bin/python

from subprocess import call

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

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

运行.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()

每次运行它都会抛出这个错误,我不知道为什么

It keeps throwing this error every time I run it and I cant figure out why

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

有什么想法吗?

推荐答案

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

You are only passing the file name of the wav file and not including the Music directory name.
Also note the change to the call statement, the filename is in a double set of quotes to handle file names with spaces and (obviously) ensure the 2 scripts are executable.

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     

运行.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()

这篇关于子进程调用不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆