try/except with multiple files

R

Robert Hicks

Is it good practice to do something like:

try:
f1 = file('file1')
f2 = file('file2')
except:
# catch the exception

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

Robert
 
M

Matimus

It depends, what are you going to do if there is an exception? If you
are just going to exit the program, then that works fine. If you are
going to just skip that file, then the above wont work. If you are
going to return to some other state in your program, but abort the
file opening, you might want to close any files that were opened. The
closing can be taken care if in the except block, but you will have to
know which ones opened successfully.

In general I would do something like this for multiple files:

Code:
filenames = ["fname1","fname2","fname3"]
for fn in filenames:
    try:
        f = open(fn)
    except IOError:
        # handle exception
    #do something with f

But, that might not work for you if the files aren't homogeneous (each
have similar contents). If the files have distinctly different
purposes, I would just wrap them each in their own try/except block.

I rambled a bit there, but I hope it helps.

Matt
 
R

Robert Hicks

It depends, what are you going to do if there is an exception? If you
are just going to exit the program, then that works fine. If you are
going to just skip that file, then the above wont work. If you are
going to return to some other state in your program, but abort the
file opening, you might want to close any files that were opened. The
closing can be taken care if in the except block, but you will have to
know which ones opened successfully.

In general I would do something like this for multiple files:

Code:
filenames = ["fname1","fname2","fname3"]
for fn in filenames:
try:
f = open(fn)
except IOError:
# handle exception
#do something with f

But, that might not work for you if the files aren't homogeneous (each
have similar contents). If the files have distinctly different
purposes, I would just wrap them each in their own try/except block.

I rambled a bit there, but I hope it helps.

Matt

Helps a bunch...thank you very much.

Robert
 
B

bruno.desthuilliers

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.
 
S

Stargaming

Matimus said:
It depends, what are you going to do if there is an exception? If you
are just going to exit the program, then that works fine. If you are
going to just skip that file, then the above wont work. If you are
going to return to some other state in your program, but abort the
file opening, you might want to close any files that were opened. The
closing can be taken care if in the except block, but you will have to
know which ones opened successfully.

In general I would do something like this for multiple files:

Code:
filenames = ["fname1","fname2","fname3"]
for fn in filenames:
try:
f = open(fn)
except IOError:
# handle exception
#do something with f

But, that might not work for you if the files aren't homogeneous (each
have similar contents). If the files have distinctly different
purposes, I would just wrap them each in their own try/except block.

I rambled a bit there, but I hope it helps.

Matt

Heh, reminded me of http://worsethanfailure.com/Articles/Code-Reuse.aspx
at the first glance. SCNR.

To add something on-topic, I'd just stick with the "one try for all
files" option if your need is really "Try to open all files, if *at
least one* of them fails, recover/abort/foobar/...".
 
S

Scott David Daniels

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 = []
try:
for name in ['abc.txt', 'def.txt', 'ghi.txt']:
files.append(open(name))
a, b, c = files
<code using the three files>
finally:
while files:
files.pop().close()

--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top