Download a file in python with urllib2 instead of urllib -
i'm trying download tarball file , save locally python. urllib it's pretty simple:
import urllib urllib2.urlopen(url, 'compressed_file.tar.gz') tar = tarfile.open('compressed_file.tar.gz') print tar.getmembers()
so question simple: what's way achieve using urllib2 library?
quoting docs:
urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]])
open url url, can either string or request object.
data
may string specifying additional data send server, ornone
if no such data needed.
nothing in urlopen
interface documentation says, second argument name of file response should written.
you need explicitly write data read response file:
r = urllib2.urlopen(url) chunk_size = 1 << 20 open('compressed_file.tar.gz', 'wb') f: # line belows downloads file @ once memory, , dumps file afterwards # f.write(r.read()) # below preferable lazy solution - download , write data in chunks while true: chunk = r.read(chunk_size) if not chunk: break f.write(chunk)
Comments
Post a Comment