我有一个python脚本,它返回有关当前正在播放的歌曲的信息:
#! /usr/bin/python3
import subprocess
p = subprocess.Popen(["/home/lucas/media/.deadbeeffiles/deadbeef", "--nowplaying", "%t"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
track = out.decode("utf-8")
p = subprocess.Popen(["/home/lucas/media/.deadbeeffiles/deadbeef", "--nowplaying", "%a"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
artist = out.decode("utf-8")
p = subprocess.Popen(["/home/lucas/media/.deadbeeffiles/deadbeef", "--nowplaying", "%b"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
album = out.decode("utf-8")
outString = "\"%s\" by %s on %s" % (track, artist, album)
q = subprocess.Popen(["notify-send", "Now Playing", outString])
但是,如果track
(或大概是artist
或album
)中包含&
,则notify-send
通知只有Now playing
没有outString
中的任何文字。我尝试在track = out.decode("utf-8")
下添加此内容:
track = track.replace("&", "\&")
但它什么都没做。然而,
track = track.replace("&", "and")
工作正常。什么可能导致它失败? subprocess.Popen
应该处理特殊字符(例如“正在播放”中的空格),并且我知道向&
添加转义并不能解决它。