当输入有&符时,Popen失败了吗?

时间:2013-10-26 21:11:11

标签: python escaping subprocess popen

我有一个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(或大概是artistalbum)中包含&,则notify-send通知只有Now playing没有outString中的任何文字。我尝试在track = out.decode("utf-8")下添加此内容:

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

但它什么都没做。然而,

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

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

0 个答案:

没有答案