why the code error?

Chris Liechti cliechti at gmx.net
Mon May 20 14:16:33 EDT 2002


lunxian at yahoo.com (lunxian) wrote in 
news:180fb736.0205191850.4aaee16 at posting.google.com:

> 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)

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

import traceback
traceback.print_exc()

>         print IOError.__str__
>         return
> 
>     for line in lines:
>         print line
> 
> if __name__ == "__main__":
>     Parser("c:\forparser.py")



-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list