Translating a Perl script into Python

Sheila King sheila at spamcop.net
Sat Jan 20 02:13:25 EST 2001


On 20 Jan 2001 09:05:23 +0200, Erno Kuusela <erno-news at erno.iki.fi> wrote in
comp.lang.python in article <kug0ied7po.fsf at lasipalatsi.fi>:

:In article <vo9i6t04nhurgv2avmgoss4mg86ofcuhtv at 4ax.com>, Sheila King
:<sheila at thinkspot.net> writes:

:| slurp = sys.stdin.read()
:| qmail = ["SENDER", "NEWSENDER", "RECIPIENT", "USER", "HOME", "HOST", "LOCAL",
:| "EXT", "EXT2", "EXT3", "EXT4", "DTLINE", "RPLINE", "UFLINE"]
:| try:
:| 	PROC = open("proc.test","w")
:| except:
:
:it's usually a bad idea to have a blanket "except:" statement,
:because it might mask other errors in your code.
:in this case i'd use
:  except (IOError, OSError), why:
:        sys.exit('Couldn't write file: %s' % why)
:
:sys.exit(), if supplied with a string, will print it to
:stderr and exit with status 1.

Thanks. I guess, I was kind of wondering about the comparison to the Perl
command, which is pretty common for opening files in Perl:

open PROC, ">proc.test" || die "Couldn't open file for write: $!";

Is the closest I can get in Python, to a Perl "die" statement, is to use
exceptions? (I'm directing this to the group, not just Erno, as I realize he
may not know Perl.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/




More information about the Python-list mailing list