[Tutor] Python equiv of Perl's 'or die()' syntax

Deirdre Saoirse deirdre@deirdre.net
Wed, 19 Jul 2000 15:46:38 -0700 (PDT)


On Wed, 19 Jul 2000, Ben Beuchler wrote:

> Hmmm... OK.  I understand how that works.  Now, since I would like to
> include that kind of syntax on almost every file I open, any thoughts on
> doing something like this:
> 
> def safeopen(filename):
> 	try:
> 		handle = open(filename)
> 		return handle
> 	except IOError:
> 		print "Opening", filename, "failed!!"
> 
> And calling it thusly:
> 
> safeopen('worthlessfile')
> 
> Would that make sense as a shortcut?

Sure, as long as you didn't later assume that the file was open. The die
syntax of perl stops execution.

-- 
_Deirdre   *   http://www.sfknit.org   *   http://www.deirdre.net
"Trust me, I'm a science fiction writer" -- Larry Niven @ Conolulu