473,387 Members | 1,721 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,387 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 1058
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.