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?
does it really matter?(I assume you are not catching this exception and it is purely for informational purposes of an individual looking at the terminal output) none of that will be seen by the operating system as such I would just
raise Exception("Invalid Arguments, expected a file that exists not %r"%(filename))
or just let it fail when it tries to open the file naturally even
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.