Ignore error with non-zero exit status (was: How to ignore error with anon-zero exit status)

Thomas 'PointedEars' Lahn PointedEars at web.de
Sun Dec 20 16:22:05 EST 2015


Ganesh Pal wrote:

> def run_scanner():
>     """
>     Mount /filesystems  and run scanner
>     """
>         for cmd in [" mount  /filesystems ", " scanner_start"]:
>         try:
>             out, err, ret = run(cmd, timeout=3600)
>             if ret != 0:
>                 logging.error("Can't run %s got %s (%d)!" % (cmd, err,
>                 ret))

Python 2.6 (why are you using the oldest Python minor version?) introduced 
string.format(), so you should use that instead of the old string format 
operator (“%”):

  logging.error("Can't run {0}; got {1} ({2:d})!".format(cmd, err, ret))

<https://docs.python.org/2.6/library/stdtypes.html#str.format>

On the other hand, you do not need the format operator to begin with:

  logging.error("Can't run %s; got %s (%d)!", cmd, err, ret)

<https://docs.python.org/2.6/library/logging.html#logging.error>

>                 return False
>         except Exception as e:
>             logging.exception("Failed to run %s got %s" % (cmd, e))

See above.

>             return False
>     logging.info("Mount /tmp.....Done !!!")
>     time.sleep(30)
> 
> 
> Iam on python 2.6 and Linux , I need  you inputs on how to ignore an
> specific error when the mount fails

(Polite people would *ask* a *question*.)
  
> In general the mount has zero status if it succeeds and anon-zero exit
> status if it fails.

(“_a non-zero_”, with a space in-between.  “anon” can be misunderstood as an 
abbreviation for “anonymous”.)
 
> 1.But for one rare case the mount succeeds but returns anon-zero exit
> status and when we get “Reading GUID from da0xxx: No such file or
> directory” error , how to ignore this error and proceed with the above
> code

If the non-zero exit status is distinguishable from other non-zero statuses, 
then you just test for that particular status code.  Otherwise, you should 
simply test if the filesystem has been mounted before you proceed.
 
> 2. Also need to add this check only for mount case i.e   mount
> /filesystems  and not scanner_start

Most simple solution for this: Do not use a loop.  More "complicated" 
solution: Use an “if” statement.

<http://www.catb.org/~esr/faqs/smart-questions.html>
 
-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.



More information about the Python-list mailing list