I'm converting several old scripts to Python for grins. If you run
the following script without a try command you get an error upon ending
espeak with a ctrl-c.
#!/bin/env python
import sys, subprocess
command=["espeak", "-s", "280"]
if len(sys.argv) > 0:
command+= sys.argv[1:]
try:
subprocess.call(command) # <- Run the espak program
print "Exiting espeak. (ctrl-d)"
except:
print "Exiting espeak. (ctrl-c)"
All this script does is run the software that reads to me called
espeak. Once you run espeak the only way to get out of it is to hit
ctrl-d or ctrl-c. The difference between those two is an error message.
The two keys mean different things. ctrl-d is "end of file" and ctrl-c
is "break".
Traceback (most recent call last):
File "say", line 8, in ?
subprocess.call(command) <- The real error
File "/usr/lib64/python2.4/subprocess.py", line 412, in call
return Popen(*args, **kwargs).wait()
File "/usr/lib64/python2.4/subprocess.py", line 1007, in wait
pid, sts = os.waitpid(self.pid, 0)
That is ugly so I gave two minutes of effort to get rid of it. Now,
I could hit ctrl-d and not get the error, but I use bash, at the house
which hitting ctrl-d at the prompt exits bash. That is a pain in the
ass and I don't always look before I hit keys so I need to CMA.
When you run the script now, if you hit ctrl-c an error happens at
the espeak command so you never get to the print right below it, only
the print after the 'except' is executed. If you hit ctrl-d there is no
error and the print straight below the espeak command is executed and
the 'except' section is ignored.
No more ugly message.
No comments:
Post a Comment