On 10/20/05, Dan Klose <[EMAIL PROTECTED]> wrote:
>
> I usually use perl but fancy a bit of a change so have started playing
> with python.

congrats!  :-)

> using perl to open a file or directory I usually use:
> open(FILE, $file) or die "Error: $!\n";
>
> The $! is a perl variable that holds an error should the open fail,
> example being : "No such file or directory".
>
> With python I have use a try except block but I have no idea how I would
> get the same warning from python as I would from perl (the reason for
> the exception), I am not even sure if this is possible (I assume it must
> be) as google searching has been fruitless.

in the body of your code, you'd have something like:

try:
    f = open(filename, 'r')
except IOError, e:
    print 'Error: %s' % str(e)  # 'e' is a exception instance obj w/
the error string
    return # or sys.exit(1) or whatever you like

hope this helps,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2006,2001
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to