python try可以使用多次吗?,pythontry可以使用,try: do s


try:    do sometingexcept:    os._exit(0)

比如执行do someting 时有错误,我想再试两次,如果两次执行还是有错误,那就os._exit(0) 退出

我找到方法了,分享出来

import osattempts = 0success = Falsewhile attempts < 3 and not success:    try:do somethingsuccess = True     except:attempts += 1if attempts==3:    os._exit(0) 

呃,这样?

def retry_do(func, retry=None):  try:    func()  except:    if retry:      retry()    else:      os._exit(0)def do_sth():  #do somethingretry_do(do_sth, retry_do(do_sth, retry_do(do_sth)))

推荐使用retrying这个packege,不要脸地推一下我自己写的介绍。

编橙之家文章,

评论关闭