0
#!/usr/bin/env python3
from os import system

def playSong():
    message = "it's time to drink water and, take a rest for some minutes."
    #filepath = "/home/leader/Downloads/anywhere.mp3"
    system("espeak '" + message + "'")

playSong()

在终端中运行此程序时,它会显示此错误。我怎样才能摆脱这个?

4

2 回答 2

0

永远不要使用system()- 在任何语言中,而不仅仅是 Python - 没有明确编写和审核为有效、安全的 shell 脚本的字符串,并且不能将不受控制的内容替换到其中。

当您需要传递参数时,请改用subprocess模块:

#!/usr/bin/env python3
import subprocess

def playSong():
    message = "it's time to drink water and, take a rest for some minutes."
    #filepath = "/home/leader/Downloads/anywhere.mp3"
    p = subprocess.Popen(['espeak', message])
    p.wait()

playSong()

否则,当有人试图播放该消息Do not ever run $(rm -rf ~)(或其更蓄意的恶意变体$(rm -rf ~)'$(rm -rf ~)')时,您将度过非常糟糕的一天。

于 2020-02-21T16:30:06.763 回答
0

试试这个,用 \ 转义单引号

message = "it\\'s time to drink water and, take a rest for some minutes."

system('espeak "%s"' %message)
于 2020-02-21T15:23:47.453 回答