On Wed, 29 Aug 2007 08:58:52 +0000, xavim wrote: > I am having some problems with how to do proper error handling. > One of the requirements is that the destination file should not be > created if there is any error in the processing. > > I have come out with the following code:: > > dictfile = file(dictpath, 'w') > try: > try: > dictgen(dictfile, *sources) > finally: > dictfile.close() > except error, e: > os.remove(dictpath) > sys.exit(str(e)) > except: > os.remove(dictpath) > > but it appears to me as somewhat ugly. Is there any more Pythonic > way to abstract this kind of error handling? Maybe 'with' or > decorators could be useful here?
With ``with`` you can get rid of the inner ``try``/``finally``: dictfile = open(dictpath, 'w') try: with dictfile: dictgen(dictfile, sources) except error, e: os.remove(dictpath) sys.exit(str(e)) except: os.remove(dictpath) raise Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list