[Python-de] urllib2 und tarfile

Olе Streicher ole-usenet-spam at gmx.net
Mo Mär 11 14:40:39 CET 2013


Hallo,

ich möchte gerne ein Tar-file mittels http herunterladen und on the fly
extrahieren (python 2.7). Passend erschien mir urllib2 und tarfile:

---------------------8<-------------------------------
import urllib2
import tarfile

url = "http://ftp.gnu.org/gnu/hello/hello-2.8.tar.gz"
tar = tarfile.open(fileobj = urllib2.urlopen(url))
tar.extractall()
---------------------8<-------------------------------

Das liefert ein

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "/usr/lib64/python2.7/tarfile.py", line 1656, in open
    saved_pos = fileobj.tell()
AttributeError: addinfourl instance has no attribute 'tell'

Die Dokumentation zu urllib2.urlopen sagt allerdings:

| This function returns a file-like object with two additional methods:
|
|    geturl() — return the URL of the resource retrieved, commonly used
|        to determine if a redirect was followed 
|    info() — return the meta-information of the page, such as headers,
|        in the form of an mimetools.Message instance (see Quick Reference
|        to HTTP Headers) 
|    getcode() — return the HTTP status code of the response.

Abgesehen davon, dass der Autor nicht zählen konnte: irgendwie passt
die Dokumentation nicht zur Exception, oder habe ich hier einen
Denkfehler?

Was ist die beste Lösung des angesprochenen Problems?

Viele Grüße

Ole


Mehr Informationen über die Mailingliste python-de