Try / Except string validation
I am confused about how to use Try/Exception
and if/else
. How can I write idiomatic code if I want to tell the user to provide a .html
file
if url[-4:] ==".html":
// do your things
else:
print('Error! the file is not html file')
I am checking to see if it should be used Try/Exception
in scenarios like this or if/else
like me.
source to share
In Python, it is easier to ask for forgiveness than permission . In other words, the idiomatic way in Python would be to just throw an exception and respond appropriately, instead of explicitly checking the condition (“Look before you jump”, also in the linked glossary). Therefore, your code should look like this:
try:
# do your thing with `url`
except:
print('Error! the file is not html file')
source to share
If you want the filename to end with ".html", it's ok to test using if.
You can also use assert if you want to throw an exception to catch it in the code several levels higher:
assert url.lower().endswith(".html"), u"the file name must end in .html"
It's just syntactic sugar for:
if url.lower().endswith(".html"):
do_your_things_with_url(url)
else:
raise YourCustomException('the url must end in ".html"')
Of course it would be silly to replace a simple test with the if
following:
try:
assert url.lower().endswith(".html")
except AssertionError:
print('Error! url does not end in ".html"')
else:
do_your_things_with_url(url)
So, to answer your question, you should probably use a test if
case to test if the string ends with ".html".
PS: This style is called LBYL (look before you jump) and there is nothing wrong with Python. The alternative is EAFP (easier to ask for forgiveness than permission). Both are fine and are considered idiomatic for most situations, with a few exceptions (e.g. duck-typing , where EAFP style explicitly prefers LBYL tests using hasattr
and / or isinstance
). Don't overuse EAFP on purpose if LBYL is comparatively cheaper - why are you trying to perform an expensive operation if a cheap test will work?
source to share
Instead of checking the file extension, you try to parse the file as HTML and then show the user a helpful error if an HTML parsing exception occurs. For example using BeautifulSoup and HTMLParser :
from bs4 import BeautifulSoup
from html.parser import HTMLParseError
try:
BeautifulSoup(fetched_url_contents)
except HTMLParseError:
print("Error: you haven't given me html!")
source to share