python - How can urllib2 be used to open a URL as a File object? -


the following methods return different objects:

urllib2.urlopen("http://example.com/image.png") >> <addinfourl @ 148620236 fp = <socket._fileobject object @ 0x8db0b6c>> 

and

open("/home/me/image.png") >> <open file '/var/www/service/provider/web/test.png', mode 'r' @ 0x8da3d88> 

is possible urlopen return same type of object open returns? not want returned stream. guess file object

the 2 same file object. if see official docs, "this function returns file-like object 2 additional methods:"...

so knowing that, can use similar methods you'd use on file object, such as:

myfile = urllib2.urlopen("http://example.com/image.png") myfile.read() 

for thing images (it looks that's you're talking about), print ugly data-representation of file. can write file on disk using like

with open("mysavedpng.png",'w') w:     w.write(myfile.read()) # note if have done myfile.read() need seek start of file myfile.seek(0) 

if want manage pngs in python, use png module


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -