473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

try/except with multiple files

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

Jun 21 '07 #1
5 1910
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:

Expand|Select|Wrap|Line Numbers
  1. filenames = ["fname1","fname2","fname3"]
  2. for fn in filenames:
  3. try:
  4. f = open(fn)
  5. except IOError:
  6. # handle exception
  7. #do something with f
  8.  
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

Jun 21 '07 #2
On Jun 21, 3:11 pm, Matimus <mccre...@gmail.comwrote:
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:

Expand|Select|Wrap|Line Numbers
  1. filenames = ["fname1","fname2","fname3"]
  2. for fn in filenames:
  3.     try:
  4.         f = open(fn)
  5.     except IOError:
  6.         # handle exception
  7.     #do something with f
  8.  

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

Jun 21 '07 #3
On Jun 21, 9:00 pm, Robert Hicks <sigz...@gmail.comwrote:
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.

Jun 21 '07 #4
Matimus wrote:
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:

Expand|Select|Wrap|Line Numbers
  1. filenames = ["fname1","fname2","fname3"]
  2. for fn in filenames:
  3.     try:
  4.         f = open(fn)
  5.     except IOError:
  6.         # handle exception
  7.     #do something with f
  8.  

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/...".
Jun 22 '07 #5
br*****************@gmail.com wrote:
On Jun 21, 9:00 pm, Robert Hicks <sigz...@gmail.comwrote:
>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 = []
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
sc***********@acm.org
Jun 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: x. zhang | last post by:
Hi Guys, We know that we can use <input type=file ...> to upload one file per time to the server. My question is if there are some way to upload multiple files per time to the server. (Of...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
5
by: vj | last post by:
Hi all, I am using C++Builder-5. I want to run multiple cpp files at the same time. If I use the C++Builder for running a cpp file (i.e., I just double click the cpp file, it then opens in the...
2
by: Sam | last post by:
Hi All, I have a solution which consists of multiple projects and each of these projects has their own app.config file. The problem is that all of my projects in the solution pull keys from the...
35
by: Arnaud Delobelle | last post by:
Hi all, Imagine I have three functions a(x), b(x), c(x) that each return something or raise an exception. Imagine I want to define a function that returns a(x) if possible, otherwise b(x),...
10
by: kimiraikkonen | last post by:
Hi, I have an app which has a listbox and when i double click an associated fileS, i want their paths to be added into listbox in my application. This code works good when i try to open a...
3
by: Gary | last post by:
Hi all, I am writing an application which will intake arguments of filenames for processing: e.g. MyApp.exe "C:\abc.txt" "C:\def.doc" - then, MyApp will process the 2 files by parsing the...
43
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail...
4
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.