从 Python 程序写入 FIFO
我试图从 python 程序控制 mplayer 的音量。 mplayer 程序从 bash 脚本启动:
#!/bin/bash
mkfifo /home/administrator/files/mplayer-control.pipe
/usr/bin/mplayer -slave -input file=/home/administrator/files/mplayer-control.pipe /home/administrator/music/file.mp3
然后我有一个用 Python 编写的 GUI,它应该能够控制正在播放的 mplayer 实例的音量。我尝试过以下方法:
os.system('echo "set_property volume $musicvol" > /home/administrator/files/mplayer-control.pipe')
如果我用数值替换 $musicvol ,则可以,但不幸的是,这没有用。我需要能够传递变量。
我还可以通过从 Python 应用程序调用 bash 脚本来解决这个问题,但我也无法让它工作:
subprocess.call("/home/administrator/files/setvolume.sh", executable="bash", shell=True)
I a trying to control the volume of mplayer from a python program. The mplayer program gets started from a bash script:
#!/bin/bash
mkfifo /home/administrator/files/mplayer-control.pipe
/usr/bin/mplayer -slave -input file=/home/administrator/files/mplayer-control.pipe /home/administrator/music/file.mp3
Then I have a GUI written in Python that is supposed to be able to control the volume of the instance of mplayer that is being played. I have tried the following:
os.system('echo "set_property volume $musicvol" > /home/administrator/files/mplayer-control.pipe')
That works if i substitute $musicvol with the numeric value instead, but that is unfortunately of no use. I need to be able to pass the variable.
I would also be able to solve it by invoking a bash script from the Python application, but I can not get that to work either:
subprocess.call("/home/administrator/files/setvolume.sh", executable="bash", shell=True)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要调用 os.system 并调用 shell 将该行从 Python 脚本写入 FIFO - 您只需执行以下操作:
我不清楚您期望在您的代码中发生什么不过,原始的 python - 环境中是否设置了musicvol?相反,如果您想要插入到要传递的字符串中的 Python 变量,最简单的方法是使用字符串插值运算符 (
%
),就像我在上面的示例中所做的那样。在使用
subprocess.call
的示例中,如果setvolume.sh
,则不需要executable
或shell
关键字参数> 是可执行的,并且有一个#!
行 - 你可以这样做:但是,最好像上面那样在 Python 中使用
open
和write
, 我认为。You don't need to call
os.system
and invoke a shell to write that line to the FIFO from your Python script- you can just do:It's not clear to me what you expect to happen in your original python, though - is
musicvol
set in the environment? If instead it's a Python variable that you want to insert into the string that you're passing, the easiest way is to use the string interpolation operator (%
) as I've done in the example above.In your example of using
subprocess.call
you don't need theexecutable
orshell
keyword arguments ifsetvolume.sh
is executable and has a#!
line - you could just do:However, it's better to just use
open
andwrite
in Python as above, I think.