How to import whole namespace into global symbol table? (newbie)
Edward Elliott
nobody at 127.0.0.1
Thu Apr 27 20:57:06 EDT 2006
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.
More information about the Python-list
mailing list