Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'nested': 0.07; 'think,': 0.07; '%s",': 0.09; 'read-only': 0.09; 'received:209.85.219': 0.09; 'throws': 0.09; 'try:': 0.09; 'subject:question': 0.10; 'def': 0.12; 'blocks': 0.16; 'class:': 0.16; 'filesystem,': 0.16; 'ioerror': 0.16; 'open()': 0.16; 'oserror': 0.16; 'to:name:python list': 0.16; "skip:' 30": 0.19; 'work,': 0.20; 'this?': 0.23; 'error': 0.23; 'handling': 0.26; 'skip:" 30': 0.26; 'this:': 0.26; 'subject:/': 0.26; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'file': 0.32; 'class': 0.32; 'stuff': 0.32; 'there,': 0.34; 'received:209.85': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'data,': 0.36; 'false': 0.36; 'leads': 0.36; 'should': 0.36; 'received:209': 0.37; 'skip:o 20': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'how': 0.40; 'catch': 0.60; 'simply': 0.61; 'more': 0.64; 'itself?': 0.84; 'subject:try': 0.84; 'system:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:from:date:message-id:subject:to :content-type; bh=gqMVsF5b6WEWOr9SX6cSJYzKnOZphEgtSv2G9lC/Pbk=; b=AWOWxzk0SpnZDTLKzq7UaLTggZPVvzUMwwmWudR3fhQ3o4/7SXxCza62nME6yzhe6l rrgcaEAcA0XtquKPdZJ3xWz6rOY6lJzWxI1i808J9tzcZDIkCXpJozDYem75RjkkwSIU sSD6NbVzEVElnQOGDaP6wYrqmTh2/Y5TJoyA/EeOEXBHxVIb4JQUIUQJAMuW9e9Wq67T NN8Mb6onAGoa3ZdNZO+cCDV65DX64LyrDD9PrqlC175qTU00aBm4ZewX9YQERK7AQwSJ C7FNh1LYf2tHBVfYQdvpNnjSbURdbNFs1sMDkc+1i6d7qpgkcwczdG2eJJj9YLadmBlr xMxg== X-Received: by 10.60.94.171 with SMTP id dd11mr1302739oeb.76.1367546089100; Thu, 02 May 2013 18:54:49 -0700 (PDT) MIME-Version: 1.0 From: J Date: Thu, 2 May 2013 21:54:29 -0400 Subject: question about try/except blocks To: Python List Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1367546098 news.xs4all.nl 15927 [2001:888:2000:d::a6]:51973 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44652 I have this function in a class: def write_file(self, data, dest): with open(dest, 'wb', 0) as outfile: try: print("IN WRITE_FILE") outfile.write(self.data) except IOError as exc: logging.error("Unable to write data to %s: %s", dest, exc) return False else: outfile.flush() os.fsync(outfile.fileno()) return True Which is simply called like this: test.write_file(test.data, target_file) However, when this is called on a read-only filesystem, it throws an OSError: File "./scripts/removable_storage_test", line 118, in write_file with open(dest, 'wb', 0) as outfile: OSError: [Errno 30] Read-only file system: '/media/bladernr/5747-AD2E/tmpfbzsxk.0' So what I am not sure of is how to handle this? Would it be better to wrap the call and catch the OSError there, or wrap the whole with open() block in the function itself? My thought is to wrap the with open() call in the function so that I'm not wrapping the function call every time I use the class somewhere, but then I am not sure of that as it leads to nested try blocks like so: try: with open(dest, 'wb', 0) as outfile: try: stuff except IOError as exec: more stuff else: other stuff except OSError as exc: error handling stuff return False I think, functionally, that should work, but should nested try/except blocks be avoided?