V
Victor Hooi
Hi,
I'd like to double-check something regarding using try-except for controlling flow.
I have a script that needs to lookup things in a SQLite database.
If the SQLite database file doesn't exist, I'd like to create an empty database, and then setup the schema.
Is it acceptable to use try-except in order to achieve this? E.g.:
try:
# Try to open up the SQLite file, and lookup the required entries
except OSError:
# Open an empty SQLite file, and create the schema
My thinking is that it is (easier to ask forgiveness than permission), but I just wanted to check if there is a better way of achieving this?
I'd also be doing the same thing for checking if a file is gzipped or not - we try to open it as a gzip, then as an ordinary text file, and if that also fails, raise a parsing error.
Cheers,
Victor
I'd like to double-check something regarding using try-except for controlling flow.
I have a script that needs to lookup things in a SQLite database.
If the SQLite database file doesn't exist, I'd like to create an empty database, and then setup the schema.
Is it acceptable to use try-except in order to achieve this? E.g.:
try:
# Try to open up the SQLite file, and lookup the required entries
except OSError:
# Open an empty SQLite file, and create the schema
My thinking is that it is (easier to ask forgiveness than permission), but I just wanted to check if there is a better way of achieving this?
I'd also be doing the same thing for checking if a file is gzipped or not - we try to open it as a gzip, then as an ordinary text file, and if that also fails, raise a parsing error.
Cheers,
Victor