How do I sort out (distinguish) an error derived from a “disk full condition” from “trying to write to a read-only file system”?
I don’t want to fill my HD to find out 🙂
What I want is to know who to catch each exception, so my code can say something to the user when he is trying to write to a ReadOnly FS and another message if the user is trying to write a file in a disk that is full.
How do I sort out (distinguish) an error derived from a disk full condition
Share
Once you catch
IOError, e.g. with anexcept IOError, e:clause in Python 2.*, you can examinee.errnoto find out exactly what kind of I/O error it was (unfortunately in a way that’s not necessarily fully portable among different operating systems).See the errno module in Python standard library; opening a file for writing on a R/O filesystem (on a sensible OS) should produce
errno.EPERM,errno.EACCESor better yeterrno.EROFS(“read-only filesystem”); if the filesystem is R/W but there’s no space left you should geterrno.ENOSPC(“no space left on device”). But you will need to experiment on the OSes you care about (with a small USB key filling it up should be easy;-).There’s no way to use different
exceptclauses depending on errno — such clauses must be distinguished by the class of exceptions they catch, not by attributes of the exception instance — so you’ll need an if/else or other kind of dispatching within a singleexcept IOError, e:clause.