Issue
I have this code here
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()
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
Any Ideas?
Solution
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
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()
Answered By - Rolf of Saxony