The standard python library has a module called commands with three nice simple functions to run a command and get its output and its status.
The first one is called commands.getstatusoutput(cmd)
, it runs the given
command and returns its exit status and its output.
The second one is called commands.getoutput(cmd)
, it runs the given command
and returns its output, ignoring the exit status.
The third one is called commands.getstatus(cmd)
, it runs the given command
and returns its exit status, ignoring the output.
Actually, no.
The third one is called getstatus(file)
, it runs the command ls -ld file
(with proper shell escaping) and returns its output.
>>> commands.getstatusoutput("echo ciao")
(0, 'ciao')
>>> commands.getoutput("echo ciao")
'ciao'
>>> commands.getstatus("echo ciao")
'ls: echo ciao: No such file or directory'
>>> commands.getstatus("/bin/echo")
'-rwxr-xr-x 1 root root 19304 2006-08-31 14:25 /bin/echo'
Every time I think of this getstatus
, I quickly convince myself that I
misread the documentation. Then I rerun the example above.