简体   繁体   English

使用raw_input时,while循环中存在Python EOFError

[英]Python EOFError in while loop when using raw_input

I can't figure out how to avoid the EOFError on the second iteration of the while loop right after prompting for raw_input. 我无法弄清楚如何在提示raw_input之后的while循环的第二次迭代中避免EOFError。 I need this loop to run 30 times and ideally wouldnt have a try-except block that would boot me from the program. 我需要此循环运行30次,理想情况下不会有一个try-except块来从程序引导我。 Any help would be appreciated!` 任何帮助将不胜感激!

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()

` `

the output is ` 输出是

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

` `

Have you tried construcing a minimal working example that reproduces the error? 您是否尝试过构造一个最小的可重现该错误的示例? I am running python on windows, so I can't try everything you are doing, as fcntl is unix specific, but I would suggest that you start removing parts of your code (like reading and playing the file) until the error disappears. 我正在Windows上运行python,所以我无法尝试所有操作,因为fcntl是unix专用的,但是我建议您开始删除部分代码(例如读取和播放文件),直到错误消失。

Also, you are opening files and streams but never explicitely close them, eg you are calling stream = p.open(...) , but there's no stream.stop_stream() and stream.close() , as well as no wf.close() . 此外,您打开文件和流,但从来没有明确地关闭它们,例如,你在呼唤stream = p.open(...)但没有stream.stop_stream()stream.close()以及不wf.close() This is probably not related to your error, but it is good practice to close files and not to rely on garbage collection. 这可能与您的错误无关,但是,良好的做法是关闭文件而不依赖垃圾回收。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM