why the code error?
Tim Roberts
timr at probo.com
Mon May 20 02:50:46 EDT 2002
lunxian at yahoo.com (lunxian) wrote:
>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.
>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.
> 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.
>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.
--
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list