Simple Question : files and URLLIB

Terry Reedy tjreedy at udel.edu
Tue Oct 14 12:35:06 EDT 2003


"Richard Shea" <richardshea at fastmail.fm> wrote in message
news:282f826a.0310140203.d890865 at posting.google.com...
> Hi - I'm new to Python. I've been trying to use URLLIB and the
'tidy'
> function (part of the mx.tidy package). There's one thing I'm having
> real difficulties understanding. When I did this ...
>
> finA= urllib.urlopen('http://www.python.org/')
> foutA=open('C:\\testout.html','w')
> tidy(finA,foutA,None)
>
> I get ...
>
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
>   File "mx\Tidy\Tidy.py", line 38, in tidy
>     return mxTidy.tidy(input, output, errors, kws)
> TypeError: inputstream must be a file object or string
>
> ... what I don't understand is surely the result of a urllib is a
file
> object ? Isn't it ? To quote the manual at :
>
> http://www.python.org/doc/current/lib/module-urllib.html
>
> "If all went well, a file-like object is returned".

'file-like object' is different from 'file object'  From urllib.py doc
string:
"The object returned by URLopener().open(file) will differ per
protocol.  All you know is that is has methods read(), readline(),
readlines(), fileno(), close() and info()."

Why this is not good enough for mx.tidy is a question for it's author.

> I can make the tidy function happy by changing the code to read ...
>
> finA= urllib.urlopen('http://www.python.org/').read()
>
> ... I haven't had time to look into this properly yet but I suspect
> finA is now a string not a file handle ?

Yes.  So it meets the 'file or string' requirement.

Terry J. Reedy






More information about the Python-list mailing list