C ha perror e errno, che stampano e memorizzano l'ultimo errore riscontrato. Questo è utile quando si esegue il file io poiché non è necessario fstat() ogni file che non funziona come argomento per fopen() per presentare all'utente un motivo per cui la chiamata non è riuscita.Python Gestione delle eccezioni
Mi chiedevo quale sia il modo corretto per catturare errno quando si gestisce con garbo l'eccezione IOError in python?
In [1]: fp = open("/notthere") --------------------------------------------------------------------------- IOError Traceback (most recent call last) /home/mugen/ in() IOError: [Errno 2] No such file or directory: '/notthere' In [2]: fp = open("test/testfile") --------------------------------------------------------------------------- IOError Traceback (most recent call last) /home/mugen/ in() IOError: [Errno 13] Permission denied: 'test/testfile' In [5]: try: ...: fp = open("nothere") ...: except IOError: ...: print "This failed for some reason..." ...: ...: This failed for some reason...
meglio della risposta accettata! – RichVel
'ioex.strerror' sembra essere l'equivalente di' os.strerror (ioex.errno) '(python 2.7) – Dannid