try/except with multiple files

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Thu Jun 21 16:46:22 EDT 2007


On Jun 21, 9:00 pm, Robert Hicks <sigz... at gmail.com> wrote:
> Is it good practice to do something like:
>
> try:
>     f1 = file('file1')
>     f2 = file('file2')
> except:
>     # catch the exception

It's bad practice. Because you use a bare except clause, and don't do
anything useful with the exceptions you catch.

> Or do you do a try/except for each open?

If what you want is to make sure that resources will be released, you
can use a try/finally block or (Python 2.5) a with block.






More information about the Python-list mailing list