[Tutor] Python equiv of Perl's 'or die()' syntax
Ben Beuchler
insyte@emt-p.org
Wed, 19 Jul 2000 17:47:47 -0500
On Thu, Jul 20, 2000 at 12:22:42AM +0200, Remco Gerlich wrote:
> To catch exceptions, you use the try: except: construct, like
>
> try:
> f = open("filethatdoesnotexist","r")
> except IOError:
> print "File open failed!"
> # Do the rest of your error handling...
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?
Ben
--
Ben Beuchler insyte@bitstream.net
MAILER-DAEMON (612)-321-9290 x101
Bitstream Underground www.bitstream.net