Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'handler': 0.05; 'that?': 0.05; 'context': 0.07; 'try:': 0.09; '"with"': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'ioerror': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:exception': 0.16; 'wrote:': 0.18; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'handling': 0.26; '(for': 0.26; 'header:In-Reply-To:1': 0.27; 'specifically': 0.29; "i'm": 0.30; 'that.': 0.31; 'file:': 0.31; 'location,': 0.31; 'file': 0.32; 'could': 0.34; 'subject:with': 0.35; 'except': 0.35; 'received:84': 0.35; 'subject:?': 0.36; 'hi,': 0.36; 'should': 0.36; 'example,': 0.37; 'skip:o 20': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'above,': 0.60; 'catch': 0.60; 'simple': 0.61; 'different': 0.65; 'holding': 0.65; 'header :Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'reply- to:addr:python.org': 0.84; 'subject:Using': 0.84; 'victor': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=PIY2p5aC c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=05MOGALpfCEA:10 a=wwHGB2F97R8A:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=cNyCHwnJM8oA:10 a=dOvacu12dNiFjNsb-tUA:9 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Tue, 22 Oct 2013 03:04:31 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Using "with" context handler, and catching specific exception? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1382407479 news.xs4all.nl 15960 [2001:888:2000:d::a6]:35653 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:57220 On 22/10/2013 02:43, Victor Hooi wrote: > Hi, > > I suspect I'm holding > > How should I use the "with" context handler as well as handling specific exceptions? > > For example, for a file: > > with open('somefile.log', 'wb') as f: > f.write("hello there") > > How could I specifically catch IOError in the above, and handle that? Should I wrap the whole thing in a try-except block? > Yes, it's as simple as that. > (For example, if I wanted to try a different location, or if I wanted to print a specific error message to the logfile). > > try: > with open('somefile.log', 'wb' as f: > f.write("hello there") > except IOError as e: > logger.error("Uhoh, the file wasn't there"). >