When a program receives a filename argument that does not exist, or is not a directory, I want to raise an error. But what error is considered best practice?
I understand that ValueError
is often used to signal invalid arguments (and I've seen several questions about it). I also understand that, especially after the reorganization of exceptions in Python 3.3 (PEP 3151), OSError
is the catch-all category for problems related to interaction with the system.
So, I have a program that expects a filename argument. If the name supplied by the caller does not exist, or exists but is a directory, what error should I raise? It's an incorrect argument so it seems that ValueError
applies; but if I try to read from it as a file, I will get an OSError
-- so shouldn't this be returned for consistency?