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()

while running this program in terminal it shows this error. How can i get rid of this?

  • 1
    Your passing a single-quoted string to `espeak`, but your single quoted string includes an apostrophe in the first word, which closes the single-quotes. There's probably a way to escape it or use different quotes, but I'm not familiar with espeak so I don't know how to avoid the problem other than by not having an apostrophe in your string. – khelwood Feb 21 '20 at 15:23
  • do you want the message to be quoted by ' and not "? – gamer Feb 21 '20 at 15:38
  • actually quote is not the problem . use of ' or '' both is okay but its still not working. – gaurav devkota Feb 21 '20 at 15:57
  • Don't use `system()` at all -- it's outright dangerous. – Charles Duffy Feb 21 '20 at 16:29
  • BTW, which version of Python? If it's a new enough release to have `subprocess.run()`, that's a bit easier to use than invoking `Popen()` directly. – Charles Duffy Feb 21 '20 at 16:53
  • yeah .it's 3.7 . i have added a new process to play a song using mpg123 and its working fine. But when i use this program to run on crontab for every n minutes song doesnt play and only the espeak program executes. Is there any idea why it's not working? – gaurav devkota Feb 21 '20 at 17:14
  • Depends on a lot of details, particularly going into which backends mpg123 is compiled to support, which sound servers (if any) your operating system runs without a desktop user logged in, etc. Which is to say -- it's a more fact-intensive (and OS/distro-specific) question that is typically a good fit here. – Charles Duffy Feb 21 '20 at 17:17
  • Honestly, my first place to start for "why doesn't mpg123 work from cron on distro-X?" would be a specific help site for distro X; for Ubuntu, for example, there's [Ask Ubuntu](https://askabuntu.com/). That way folks will know how mpg123 is compiled, which sound servers (if any) are used out-of-the-box and how they're configured, etc. – Charles Duffy Feb 21 '20 at 17:18
  • yeah. thanks for the help. – gaurav devkota Feb 21 '20 at 17:23

2 Answers2

0

try this, escape single quote with \

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

system('espeak "%s"' %message)
Wonka
  • 1,548
  • 1
  • 13
  • 20
  • ok have to use 2 \\. Test it please, it work on my little lab and use the edited way to lauch system – Wonka Feb 21 '20 at 16:01
  • it worked but partially. only " it's " was executed. had to put " quote on both sides and escape it. message = "\"it\\'s time to drink water and, take a rest for some minutes.\"" – gaurav devkota Feb 21 '20 at 16:26
  • Check it new update as you suggest. I tested with a file 0'0.txt and simple cat on previous test – Wonka Feb 21 '20 at 16:28
0

Do not ever use system() -- in any language, not just Python -- without a string that has been explicitly written and audited to be valid, safe shell script, and which cannot have uncontrolled contents substituted into it.

When you need to pass arguments through, use the subprocess module instead:

#!/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()

Otherwise, you would have a very bad day when someone tries to play the message Do not ever run $(rm -rf ~) (or its more deliberately-malicious variant $(rm -rf ~)'$(rm -rf ~)').

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441