try/except with multiple files

Scott David Daniels scott.daniels at
Mon Jun 25 04:57:16 CEST 2007

bruno.desthuilliers at wrote:
> On Jun 21, 9:00 pm, Robert Hicks <sigz... at> wrote:
>> Is it good practice to do something like:
>> try:
>>     f1 = file('file1')
>>     f2 = file('file2')
>> except:
>>     # catch the exception
> 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.

You could do something like this:

     files = []
         for name in ['abc.txt', 'def.txt', 'ghi.txt']:
         a, b, c = files
         <code using the three files>
         while files:

--Scott David Daniels
scott.daniels at

More information about the Python-list mailing list