473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Simple Question : files and URLLIB

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/li...le-urllib.html

"If all went well, a file-like object is returned". I can make the
tidy function happy but 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 ?

Anyway if anyone can throw light on this I would be grateful.

thanks

richard.shea.
Jul 18 '05 #1
4 3961
> "If all went well, a file-like object is returned". I can make the

file-like means having similar interface to a file object (methods read,
readline, etc.), but not a real file though,

mxTidy.tidy most probably requires a real file to be passed,
just you look into Tidy.py (line 38) and you'll know for sure

--
bromden[at]gazeta.pl

Jul 18 '05 #2
> 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 ?


Correct. If you do:
print type(finA)
you obtain the result:
<type 'str'>

If you do:
finA= urllib.urlopen('http://www.python.org/')
print type(finA)
then you obtain the result:
<type 'instance'>

Compare this with:
finA = open("blah", "w")
print type(finA)
which gives the result:
<type 'file'>

According to the docs on urlopen( url[, data[, proxies]]) :
"If all went well, a file-like object is returned."
So the answer would appear to be: "close, but no cigar".
Jul 18 '05 #3

"Richard Shea" <ri*********@fastmail.fm> wrote in message
news:28*************************@posting.google.co m...
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/li...le-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
Jul 18 '05 #4
Thanks to everyone for the info/feedback. In particular I didn't know
you could that ...

type(finA)

.... business (which shows you how new to Python I am probably) but
it'll come in handy.

As I think you realised I had misunderstood exactly what urllib was
offering however the blah.read() approach is quite good enough. Just
out of curiousity though if 'tidy' demanded a file (rather than being
prepared to take a string as it is)would the only sure approach be to
....

f1=open('C:\\workfile.html','w')
strHTML= urllib.urlopen('http://www.python.org/').read()
f1.write(strHTML)
tidy(f1,strOut,None)

.... that is to take the string that results from the read on urllib
file-like object and write it back out to a file ?

Just wondering ...

Thanks again for the information on my original question.

regards

richard.

ri*********@fastmail.fm (Richard Shea) wrote in message news:<28*************************@posting.google.c om>...
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/li...le-urllib.html

"If all went well, a file-like object is returned". I can make the
tidy function happy but 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 ?

Anyway if anyone can throw light on this I would be grateful.

thanks

richard.shea.

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Blaktyger | last post by:
I would like to download some mp3 files from a web site. There is to much of them and I had the idea of writing a script to do it for me. Code: import string import urllib f =...
3
by: sj | last post by:
I wish to download hundreds of files from the University of Iowa sound archive. Doing it manually would be a daunting task especially since the files are each a few mega bytes long. Is there a...
1
by: Timothy Wu | last post by:
Hi, I'm trying to fill the form on page http://www.cbs.dtu.dk/services/TMHMM/ using urllib. There are two peculiarities. First of all, I am filling in incorrect key/value pairs in the...
0
by: Ray Slakinski | last post by:
I got a small issue, I am using urllib.urlretreive to download files but in some cases I'm downloading from a CGI that is redirecting urlretrieve to a different url. Example: ...
6
by: justsee | last post by:
Hi, I'm using Python 2.3 on Windows for the first time, and am doing something wrong in using urllib to retrieve images from urls embedded in a csv file. If I explicitly specify a url and image...
7
by: Shani | last post by:
I have the following code which takes a list of urls "http://google.com", without the quotes ofcourse, and then saves there source code as a text file. I wan to alter the code so that for the list...
7
by: Ehsan | last post by:
I foundd this code in ASPN Python Cookbook for downloading files in python but when it finished downloading files the files became corrupted and didn't open, the files in internet havn't any...
5
by: chrispoliquin | last post by:
Hi, I have a small Python script to fetch some pages from the internet. There are a lot of pages and I am looping through them and then downloading the page using urlretrieve() in the urllib...
4
by: Clay Hobbs | last post by:
I am making a program that (with urllib) that downloads two jpeg files and, if they are different, displays the new one. I need to find a way to compare two files in Python. How is this done? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.