I just ran a small Python script of mine on a new computer and I got an error message that xsltproc isn't found. So I want to modify the script to test whether xsltproc is installed. (I should probably do this with several other scripts too.)
However, I don't know how to do this. How do I learn, from a Python script running on Linux, whether a program is installed?
>>1
You could always run `which xsltproc`, however that's done in Python. If it returns 1, it's not found, if 0 then all is well.
Name:
Anonymous2008-01-07 21:55
>>4 You mean split $PATH by os.pathsep and look in each dir.
In code,
import os
f = 'xsltproc'
for d in os.environ.get('PATH', os.defpath).split(os.pathsep):
path = os.path.join(d, f)
if os.path.exists(path):
break
else:
path = None
print path or 'Not found.'
Name:
Anonymous2008-01-07 22:37
>>6
Yes, that's a much better idea than doing it the right way (>>5).
OP here. I only use XSLT to transform XHTML documents into HTML-compatible documents. I'm not a fan of XSLT.
Today I was looking through Python's docs and I learned of a much easier solution. I haven't spent much time with Python, so I never heard of the subprocess module. http://docs.python.org/lib/module-subprocess.html
It throws an exception when a program isn't installed, which for me is a good solution. So I ended up just changing os.system(...) calls to subprocess.call(...) calls. Now the script stops executing when a command fails, instead of barging on like before.