why the code error? 
Author Message
 why the code error?

i'm a newbie of python. i encounter a problem in my programming.
here is my program. when it's running, a IOError always raised.
why? and how can i get more detail error message of the exception?

import os
import sys

def Parser(filename):
    lines = None
    try:
        fsock = file("forparser.py","r",0)
        try:
            lines = fsock.readlines()
        finally:
            fsock.close()
    except IOError:
        print IOError.__str__
        return

    for line in lines:
        print line

if __name__ == "__main__":
    Parser("c:\forparser.py")



Fri, 05 Nov 2004 10:50:05 GMT  
 why the code error?
The exception syntax you used is correct, but it does not
assign the exception to a variable.
You just need to add a variable to your except statement.
For the except section you want it to look like this:
    except IOError, e:
        print str(e)
        return
So the way you used it you cannot get specific information
about the exception.

In addition you could import traceback and print the stack.
This may give you more interesting information.
It would look something like this:
    import traceback
    ...
    try:
        ...
    except IOError, e:
        print str(e)
        traceback.print_exc()
        return

Yours,
Noah

Quote:
-----Original Message-----


Sent: Sunday, May 19, 2002 7:50 PM

Subject: why the code error?

i'm a newbie of python. i encounter a problem in my programming.
here is my program. when it's running, a IOError always raised.
why? and how can i get more detail error message of the exception?

import os
import sys

def Parser(filename):
    lines = None
    try:
        fsock = file("forparser.py","r",0)
        try:
            lines = fsock.readlines()
        finally:
            fsock.close()
    except IOError:
        print IOError.__str__
        return

    for line in lines:
        print line

if __name__ == "__main__":
    Parser("c:\forparser.py")



Fri, 05 Nov 2004 14:09:00 GMT  
 why the code error?

Quote:

>i'm a newbie of python. i encounter a problem in my programming.
>here is my program. when it's running, a IOError always raised.
>why? and how can i get more detail error message of the exception?

If you get rid of the try/except, the error message will give you more
information.

Quote:
>import os
>import sys

>def Parser(filename):
>    lines = None
>    try:
>        fsock = file("forparser.py","r",0)

I assume you meant file(filename,"r") here, but put the file name in
directly for testing.  Does "forparser.py" exist in the current directory?
Otherwise, I don't see anything wrong with this.

Quote:
>        try:
>            lines = fsock.readlines()
>        finally:
>            fsock.close()
>    except IOError:
>        print IOError.__str__
>        return

>    for line in lines:
>        print line

I would have written all of that as:

    for line in open('filename').readlines():
        print line

but that's just personal preference.

Quote:
>if __name__ == "__main__":
>    Parser("c:\forparser.py")

Backslashes have special meaning.  You need to use one of these:
    Parser("c:\\forparser.py")
    Parser(r"c:\forparser.py")
    Parser("c:/forparser.py")

Otherwise, the \f will be changed to a formfeed -- 0x0C.
--

  Providenza & Boekelheide, Inc.



Fri, 05 Nov 2004 14:50:46 GMT  
 why the code error?


Quote:
> i'm a newbie of python. i encounter a problem in my programming.
> here is my program. when it's running, a IOError always raised.
> why? and how can i get more detail error message of the exception?

> import os
> import sys

> def Parser(filename):
>     lines = None
>     try:
>         fsock = file("forparser.py","r",0)

unless you have a very good reason, leve the ",0" away...
        fsock = file("forparser.py","r")

(and "r" is the default, if you want to save typing)

Quote:
>         try:
>             lines = fsock.readlines()
>         finally:
>             fsock.close()
>     except IOError:

import traceback
traceback.print_exc()

Quote:
>         print IOError.__str__
>         return

>     for line in lines:
>         print line

> if __name__ == "__main__":
>     Parser("c:\forparser.py")

--



Sat, 06 Nov 2004 02:16:33 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Why do I get Load Error code 8 from Labview

2. why do i get error code -2147024891?

3. why error message on this form code below

4. why why why oh why why baby

5. Why this MAKE Error ->Make error: No source for objet .obj

6. what is a DOS error code 170 DBFNTX/1001 open error =

7. Error code DBFCMD 2001 : ORDLISTCLEAR() error

8. Thorw an error or return an error code

9. Why, Why Why????

10. why why why (mouse related question)

11. MVCL (was Re: why code in 370 Assembler)

12. why code in c or assembler?

 

 
Powered by phpBB® Forum Software