Je n'arrive pas à comprendre comment éviter EOFError lors de la deuxième itération de la boucle while juste après avoir demandé raw_input. J'ai besoin de cette boucle pour courir 30 fois et, idéalement, je n'aurais pas un essai-sauf le bloc qui me démarrer du programme. Toute aide serait appréciée! `Python EOFError dans la boucle while lors de l'utilisation de raw_input
import sys
import pyaudio
import wave
import os
import fcntl
import time
def main():
sound_cues = open('sound_cues').read().splitlines()
light_cues = open('light_cues').read().splitlines()
play_names = open('play_names').read().splitlines()
plays = []
for idx in range(len(play_names)):
plays.append({'name': play_names[idx], 'sound': sound_cues[idx], 'lights': light_cues[idx]})
while(True):
play_num = raw_input("Enter a play number: ")
play = plays[int(play_num) - 1]
print
print 'lights:'
print play['lights']
print
raw_input("Press Enter to start sound...")
print
print 'sound:'
print play['sound']
audio = "sounds/" + play['sound']
wf = wave.open(audio)
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output=True)
data = wf.readframes(1024)
fl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, fl | os.O_NONBLOCK)
print
print "Press Enter to stop audio..."
print
while data != '':
try:
stdin = sys.stdin.read()
if "\n" in stdin or "\r" in stdin:
break
except IOError:
pass
stream.write(data)
data = wf.readframes(1024)
if __name__ == "__main__":
main()
`
la sortie est `
Enter a play number: 2
lights:
Black out, clamp light works as spot, follows Kris
Press Enter to start sound...
sound:
1.wav
Press Enter to stop audio...
Enter a play number: Traceback (most recent call last):
File "30plays_1.py", line 56, in <module>
main()
File "30plays_1.py", line 18, in main
play_num = raw_input("Enter a play number: ")
EOFError
`
Vous mélangez l'utilisation de 'sys.stdin.read()' et 'raw_input'. Je devine après 'read()', 'stdin' devient vide et' raw_input' lance un EOFError. –