473,880 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "<interacti ve input>", line 1, in ?
File "mx\Tidy\Tidy.p y", line 38, in tidy
return mxTidy.tidy(inp ut, 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 3998
> "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*********@fa stmail.fm> wrote in message
news:28******** *************** **@posting.goog le.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 "<interacti ve input>", line 1, in ?
File "mx\Tidy\Tidy.p y", line 38, in tidy
return mxTidy.tidy(inp ut, 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().ope n(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:\\wo rkfile.html','w ')
strHTML= urllib.urlopen( 'http://www.python.org/').read()
f1.write(strHTM L)
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*********@fas tmail.fm (Richard Shea) wrote in message news:<28******* *************** ***@posting.goo gle.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 "<interacti ve input>", line 1, in ?
File "mx\Tidy\Tidy.p y", line 38, in tidy
return mxTidy.tidy(inp ut, 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
3600
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 = urllib.urlopen(""" http://www.somemp3site.com/somemp3.com""") fic=open('mp3file.mp3','w')
3
4545
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 standard way of using Python for such a task? I have a fair amount of programming experiance but very little of it relates to networks. For those who are intrested the University of Iowa's sound archive may be found at...
1
2081
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 parameters on purpose because that's the only way I can get it to work.. For "version" I am suppose to leave it unchecked, having value of empty string. And for name "outform" I am suppose to assign it a value of "-short". Instead, I left out
0
1294
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: urllib.urlretreive('http://someurl.com/files.asp?file=55', 'tempFileName.tmp') Is there a way to know what filename files.asp is redirecting to so I
6
5830
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 name it works fine(commented example in the code), but if I pass in variables in this for loop it throws errors: --- The script: import csv, urllib
7
1493
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 of URLs an html file is saved. -----begin----- import urllib urlfile = open(r'c:\temp\url.txt', 'r') for lines in urlfile: try:
7
2551
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 problem: def download(url,fileName): """Copy the contents of a file from a given URL to a local file. """ import urllib
5
13047
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 module. The problem is that after 110 pages or so the script sort of hangs and then I get the following traceback: Traceback (most recent call last):
4
4050
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? -- Ratfink
0
9774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11093
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10715
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9550
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7951
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7105
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5778
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4192
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.