Scott Simpson wrote: > def func(): > from sys import stderr, exit > try: > f = open("foo", 'r') > except IOError: > print >> stderr, "Input file foo does not exist" > exit(1)
IOError can be raised when foo exists, e.g. if there's a permission problem. It's better to print the actual error message: >>> except IOError, e: ... print >>stderr, e [Errno 13] Permission denied: 'foo' > Notice that I have two "import sys" statements, one for each function. > Is there any way to import the "sys" stuff to the global symbol table so > I don't need two "import" statements? move import outside your function scopes. > Lastly, is there an equivalent of Perl's "die" function? I'm writing to > stderr and dieing above but I'm not quite sure if this is the "correct" > way. you can reraise the exception and get a stack trace. if you want to replicate die "something\n"; (no stack trace), I'm not sure if there's a single-function python equivalent. -- http://mail.python.org/mailman/listinfo/python-list