472,371 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

with statements and exceptions

I'm thinking about using a with statement for opening a file, instead of
the usual try/except block, but I don't understand where you handle an
exception if the file doesn't open. For example:

with open('myfile', 'r'):
BLOCK

I assume that BLOCK can/will contain all the other stuff you want to do,
which may involve try/except blocks, but what if the initial open() call
fails (for lack of file, etc.)? Is this the purpose of the with
statement, to handle this itself? Is there still some way that I can
respond to this and show the user an error message?

Thanks.
Oct 25 '06 #1
2 1036
John Salerno wrote:
I'm thinking about using a with statement for opening a file, instead of
the usual try/except block, but I don't understand where you handle an
exception if the file doesn't open. For example:

with open('myfile', 'r'):
BLOCK

I assume that BLOCK can/will contain all the other stuff you want to do,
which may involve try/except blocks, but what if the initial open() call
fails (for lack of file, etc.)? Is this the purpose of the with
statement, to handle this itself? Is there still some way that I can
respond to this and show the user an error message?

Thanks.
Let me just toss this in as well:

def create_sql_script(self):
with open('labtables.sql') as sql_script:
return sql_script.read()

Does the file still get closed even though I have a return statement
inside the with block?

Thanks.
Oct 25 '06 #2
John Salerno <jo******@NOSPAMgmail.comwrites:
def create_sql_script(self):
with open('labtables.sql') as sql_script:
return sql_script.read()

Does the file still get closed even though I have a return statement
inside the with block?
Yes, I think so. I'm not running 2.5 yet but this is supposed to be
the idea of the with statement. As for the open failing, you'd deal
with that using try/except around the whole thing.

http://www.python.org/dev/peps/pep-0343/ explains the with statement
in detail.
Oct 25 '06 #3

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

Similar topics

3
by: pythos | last post by:
I have a piece of code like this: try: some code except: print >> sys.stderr, "error: ", sys.exc_info() When an exception is thrown from the code, what I see on the console is this:
6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
8
by: John B | last post by:
ARGGGGGHH I have a database class instantiated with a Using statement, then a reader also instantiated with a using statement. If the command I use to populate the reader throws an exception, then...
9
by: John Salerno | last post by:
One of the things I learned with C# is that it's always better to handle any errors that might occur within the codes itself (i.e. using if statements, etc. to catch potential out of range...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
2
by: ojorus | last post by:
Hi! Some questions regarding the mysqli-extension (php5) 1) Prepared statements: If I understand things right, prepared statements will give better performance if you make several similar...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
3
by: Doug | last post by:
Hi i have a method that returns a value public bool readxml (string xmlFilename, out string value) but I would like to catch an exception if it occurs in the method . How do i catch the...
3
by: =?Utf-8?B?WFNsZXI=?= | last post by:
I have some code that works fine, except for the fact that it has a lot of embedded if statements. I don't think that this would be practice code, but I wanted to tap into the knowledge of this...
28
by: Christoph Zwerschke | last post by:
What is the best way to re-raise any exception with a message supplemented with additional information (e.g. line number in a template)? Let's say for simplicity I just want to add "sorry" to every...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

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.