使用tarfile和urllib

时间:2017-02-01 17:13:41

标签: python ftp urllib

我试图从ftp服务器上的tar文件中下载文件。类似于这个Read contents of .tar.gz file from website into a python 3.x object,当我打开tarfile时,我得到一个ReadError(下面)

ftpURL = u'ftp://ftpprd.ncep.noaa.gov/pub/data/nccf/com/wave/prod/multi_1.20170201/multi_1.t00z.spec_tar.gz'
ftpstream = urllib.urlopen(ftpURL)
tar = tarfile.open(fileobj=ftpstream, mode='r|bz2')    # here's where i get the error 
Traceback (most recent call last):
  File "C:\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-20-c3e97355618c>", line 1, in <module>
    tar = tarfile.open(fileobj=ftpstream, mode='r|bz2')
  File "C:\Anaconda2\lib\tarfile.py", line 1703, in open
    t = cls(name, filemode, stream, **kwargs)
  File "C:\Anaconda2\lib\tarfile.py", line 1587, in __init__
    self.firstmember = self.next()
  File "C:\Anaconda2\lib\tarfile.py", line 2355, in next
    tarinfo = self.tarinfo.fromtarfile(self)
  File "C:\Anaconda2\lib\tarfile.py", line 1251, in fromtarfile
    buf = tarfile.fileobj.read(BLOCKSIZE)
  File "C:\Anaconda2\lib\tarfile.py", line 579, in read
    buf = self._read(size)
  File "C:\Anaconda2\lib\tarfile.py", line 598, in _read
    raise ReadError("invalid compressed data")
ReadError: invalid compressed data

我错过了缓冲区大小的东西吗?如果是这样,不熟悉缓冲区大小,我会在哪里找到有关所需缓冲区大小的特定信息,我将大小加倍和三倍无济于事。我还尝试了一些文件。我可以手动下载文件并在我的机器上打开....任何帮助都非常感谢

1 个答案:

答案 0 :(得分:0)

仔细看看签名:

tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)

描述:

  

如果给定,fileobj可以是任何具有read()或write()的对象   方法(取决于模式)。 bufsize指定blocksize和   默认为20 * 512字节。将此变体与例如   sys.stdin,套接字文件对象或磁带设备。但是,这样的   TarFile对象的局限性在于它不允许随机访问,请参阅   实例

你打算做的是:

tar = tarfile.open(fileobj=ftpstream, mode='r|bz2')