Proper error catching
Never, ever have an empty except
clause like the following:
try:
...
except:
...
With an empty except
clause, any error that occurs in the try
block will be caught by the except
clause. This includes errors that aren't intended to be caught, like a SystemError
, where something goes wrong internally.
The proper way to specify what error you want to catch would be to write your except
clause like this:
except ExceptionType:
...
Properly opening files
The generally accepted method for opening files in Python is to use a contest manager, as seen below. If you use a context manager, the file is closed implicitly, and you can guarantee that the file is closed properly if the program unexpectedly exits:
with open("path/to/my/file.dat", "mode") as my_file:
...
More to come.