473,394 Members | 1,416 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,394 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 1059
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: 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: 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
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...
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...

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.