與Python 3 mplayer的子我有一個非常...特定問題。真的試圖找到一個更廣泛的問題,但不能。寫命令在Windows
我想使用的mplayer作爲子進程來播放音樂(在Windows和Linux的也),並保留命令傳遞給它的能力。我已經在python 2.7中用subprocess.Popen
和p.stdin.write('pause\n')
完成了。
但是,這似乎沒有幸存下來的Python 3之旅。我不得不使用'pause\n'.encode()
或b'pause\n'
轉換爲bytes
,並且mplayer進程不會暫停。它似乎如果我使用p.communicate
不過來工作,但我已經排除了這種可能性是由於this question成爲了可能,它聲稱它只能被稱爲每個進程一次。
這裏是我的代碼:
p = subprocess.Popen('mplayer -slave -quiet "C:\\users\\me\\music\\Nickel Creek\\Nickel Creek\\07 Sweet Afton.mp3"', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
time.sleep(1)
mplayer.stdin.write(b'pause\n')
time.sleep(1)
mplayer.stdin.write(b'pause\n')
time.sleep(1)
mplayer.stdin.write(b'quit\n')
看到,因爲這個代碼工作(不b
收費)2.7,我只能假設編碼字符串作爲bytes
以某種方式修改字節的值,使得MPlayer能不再理解了嗎?然而,當我試圖看到通過管道發送什麼字節它看起來是正確的。它也可能是窗口管道奇怪。我已經用cmd.exe和powershell試過了,因爲我知道powershell將管道解釋爲xml。我用這個代碼來測試通過管道什麼進來:
# test.py
if __name__ == "__main__":
x = ''
with open('test.out','w') as f:
while (len(x) == 0 or x[-1] != 'q'):
x += sys.stdin.read(1)
print(x)
f.write(x)
和
p = subprocess.Popen('python test.py', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
p.stdin.write(b'hello there\ntest2\nq\n')
謝謝!我不久將檢查該解決方案......是的,我故意沒有使用universal_newlines,因爲它會改變我的字符串的值,但我想我甚至沒有想到的是mplayer的Windows版本可以期待一個\ r \ n,在事實上它可能是。是的,我一直在我的實際代碼中使用DEVNULL,不過謝謝你的提示。 – Nacht
換行似乎不重要,但沖洗流的作品!非常感謝...感嘆我可能應該認爲這是誠實的。哦,謝謝 – Nacht