I’m trying to read a csv-file and the actions taken after this depends on whether there is a csv-file or not.
try:
temp = pl.read_parquet("test.parquet")
do some super cool other stuff
do some additional things
etc...
except:
temp = temp = pl.read_parquet("fallback.parquet")
How is this done in Python? Try to read a file, if the file doesn’t exist read a fallback file?
with the try/except around only the read_parquet call
So something like:
try:
temp = pl.read_parquet("test.parquet")
except FileNotFoundError:
temp = pl.read_parquet("fallback.parquet")
do stuff here
We try to avoid “bare” excepts because they catch everything.
Permission errors (file exists but you can’t read it), name errors
(you’ve misspelled a variable name) etc etc. A try/except should catch only the precise circumstance which you’re accomodating. Here that is
a missing file, and there’s a specific exception for that.
We also try to keep the try/except body as short as possible. That way
you know what the exception came from. As opposed to sme exception from
your additional things.