execfile error exception line number?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Blais

    #1

    execfile error exception line number?

    Hello,

    I'd like to do the following:

    import sys
    try:
    execfile("somef ile.py")
    except:
    s=sys.exc_info( )
    print "Error '%s' happened on line %d" % (s[1],s[2].tb_lineno)


    (I'm going to replace the print later with a gui dialog)

    how can I get the line number in "somefile.p y" where the error occurs? When I do the
    above, I get the line number in the script which calls execfile instead.


    thanks,


    Brian Blais

    --
    -----------------

    bblais@bryant.e du

  • André

    #2
    Re: execfile error exception line number?


    Brian Blais wrote:[color=blue]
    > Hello,
    >
    > I'd like to do the following:
    >
    > import sys
    > try:
    > execfile("somef ile.py")
    > except:
    > s=sys.exc_info( )
    > print "Error '%s' happened on line %d" % (s[1],s[2].tb_lineno)
    >[/color]
    How about this:
    === input: ====
    try:
    execfile("somef ile.py")
    except Exception, info:
    print "Error '%s' happened on line %d" % (info[0], info[1][1])
    ==== output: ====
    Error 'invalid syntax' happened on line 3
    === somefile.py is =====
    print 3
    print 3
    prnt 5
    print 6
    =============


    André

    Comment

    Working...