C'è poca differenza tra i due tipi. Infatti, anche gli sviluppatori core di Python hanno concordato che non c'è alcuna differenza reale e rimosso IOError
in Python 3 (ora è un alias per OSError
). Vedi PEP 3151 - Reworking the OS and IO exception hierarchy:
While some of these distinctions can be explained by implementation considerations, they are often not very logical at a higher level. The line separating OSError
and IOError
, for example, is often blurry. Consider the following:
>>> os.remove("fff")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: 'fff'
>>> open("fff")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'fff'
Sì, è di due tipi di eccezione differenti con il messaggio esattamente lo stesso errore.
Per il proprio codice, attenersi al lancio OSError
. Per le funzioni esistenti, controllare la documentazione (dovrebbe dettaglio ciò che è necessario prendere), ma si può tranquillamente prendere entrambi:
try:
# ...
except (IOError, OSError):
# handle error
Citando nuovamente il PEP:
In fact, it is hard to think of any situation where OSError
should be caught but not IOError
, or the reverse.
fonte
2015-03-30 13:49:37
* Modificato nella versione 3.3: ' EnvironmentError', 'IOError',' WindowsError', 'VMSError',' socket.error', 'select.error' e' mmap.error' sono stati uniti in 'OSError'. * basta lanciare 'OSError' e dimenticare' IOError'. –
@MartijnPieters Grazie, ho aggiunto il tag Python 2. Il solo lancio di OSError mi suona bene, tuttavia ho sempre difficoltà a sapere quando una funzione come "shutil.copyfile()" o "os.access()" solleva IOError o OSError (deve sempre cercarlo) –
Vedi anche https://www.python.org/dev/peps/pep-3151/ per lo sfondo su questo, ti aiuterà a mettere le due eccezioni in prospettiva. –