Ignore stderr and use return code

Cameron Simpson cs at zip.com.au
Mon Oct 26 00:34:55 EDT 2015


On 25Oct2015 11:35, Ganesh Pal <ganesh1pal at gmail.com> wrote:
>In the below code, we found that the command i.e  cmd = "mount
>/filesystem1" succeeded. But  the test failed due to the weaker stderr
[...]
>        out, err = proc.communicate()
>        if err != "":

Checking stderr is never the correct way to do this.

[...]
> - To handle this case, Iam planning to use return code and modify the
>above code as below ( Any other suggestions please let me know)
>  def mount_me():
>        cmd = ("mount /filesystem1")
>        out, err, ret = run(cmd, timeout=60) # run is the wrapper,
>returns (stdout, stderr, returncode)
>        if ret != 0: #  zero means succeeded

This is the correct approach.

>        logging.error("Can't run %s got %s (%d)!" % (cmd, err, ret))
>            return False
>-  Do I need to add more check to ensure the mount actually succeeds,
>may be a function?

No. Mount should have a zero status if it succeeds and anon-zero exit status if 
it fails. There is no grey area here.

>        if ret != 0 and check_df_output():
>          logging.error("Can't run %s got %s (%d)!" % (cmd, err, ret))

Rather than looking at the stdout or stderr of df, consider calling 
os.statvfs() directly from Python:

  https://docs.python.org/3/library/os.html#os.statvfs

> Iam using python 2.7 on Linux

It is the same in Python 2.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list