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

N

Nicolas Fleury

Hi,
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 returns an error
in a command doesn't exist, so I'm sure we could have an exception in
the same case for popen in Python?

Thx and Regards,

Nicolas
 
N

Nicolas Fleury

Nicolas said:
Hi,
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 returns an
error in a command doesn't exist, so I'm sure we could have an exception
in the same case for popen in Python?

Small correction, as far as I can see, Perl's open returns the same
error if the command returns 1 or doesn't exist. Same problem there...
 
J

John J. Lee

Nicolas Fleury said:
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:
sh: nonsense: command not found
32512[...snip directory listing...]
0ls: nonsense: No such file or directory
256

John
 
N

Nicolas Fleury

John said:
Nicolas Fleury said:
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:

It doesn't behave like that on Windows... the return value is 1 weither
it's the return value of the command executed of if this command doesn't
exist. What would be nice, I think, would be to encapsulate the
difference, and raise an exception if the command doesn't exist. That
might implies additional code for Windows, but in the end the python
code would be more portable.

Regards,

Nicolas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,491
Latest member
mohitk

Latest Threads

Top