3 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
shutil.rmtree
OSError
:
>>> shutil.rmtree("/fake/dir") Traceback (most recent call last): [...] OSError: [Errno 2] No such file or directory: '/fake/dir'
try: shutil.rmtree(path) except OSError: pass
shutil.rmtree(2)
Exception
except:
SystemExit
sys.exit()
>>> try: ... sys.exit(1) ... except: ... pass ... >>>
>>> try: ... sys.exit(1) ... except Exception: ... pass ... shell:~$
OSError
Errno 2
try: shutil.rmtree(path) except OSError, e: if e.errno == 2: # suppress "No such file or directory" error pass else: # reraise the exception, as it's an unexpected error raise
import errno
if
if e.errno == errno.ENOENT:

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
當(dāng)您只想在不處理異常的情況下執(zhí)行TRY CATCH時(shí),如何在Python中執(zhí)行呢?
try: do_something() except: handle_exception() raise #re-raise the exact same exception that was thrown
添加回答
舉報(bào)