1

我有一个 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(或大概是artistor album)在其中有一个&,则notify-send通知只有在Now playing没有任何文本的情况下outString。我试着在下面添加这个track = out.decode("utf-8")

track = track.replace("&", "\&")

但它什么也没做。然而,

track = track.replace("&", "and")

工作正常。什么可能导致它失败?subprocess.Popen应该处理特殊字符(例如“正在播放”中的空格),我知道添加转义&并不能解决它。

4

0 回答 0