How to know if a system command doesn't exist?

John J. Lee jjl at
Mon Jan 12 22:16:50 CET 2004

Nicolas Fleury <nid_oizo at yahoo.com_remove_the_> writes:
> Nicolas Fleury wrote:
> >     How can I know if a system command doesn't exist?  All the ways
> > I have tried behave like if the system command was existing but
> > returning one.  I don't want to sound provocative, but open in Perl

Haven't tried popen(), but I assume C's system() (of which os.system()
is a trivial wrapper, on posix-y OSes) is implemented in terms of
popen() (or both are implemented in terms of similar lower-level
stuff).  I get different return codes from system() depending on
whether the program exists or not, as expected:

>>> os.system("nonsense")
sh: nonsense: command not found
>>> os.system("ls")
[...snip directory listing...]
>>> os.system("ls nonsense")
ls: nonsense: No such file or directory


More information about the Python-list mailing list