tarfile.ReadError: not a gzip file

关于tarfile读取tgz文件报ReadError:not a gzip file的问题分析

在学习机器学习过程中,需要下载一个房屋数据,按照书上的流程操作下来发现出现以下错误:

tarfile.ReadError: not a gzip file

初始认为是由于tarfile文件调用错误,经过一番查找,发现并非如此。

分析:

使用工具下载的数据包大小为139kb,无论是使用urllib.request.urlretrieve还是使用requests.get方法。

手动下载的数据包大小为400kb

点击下载地址进行下载。

错误原因猜测

通过上面的分析可知,使用工具下载的文件并不完整,虽然从表面看它是一个tgz文件,但是你可以把它理解成一个已经损坏的tgz文件,因此使用tarfile无法正确的进行打开,以至于报tarfile.ReadError: not a gzip file错误。

解决方案一

知道的错误原因后,我们可以手动下载对应的tgz文件并放在创建的文件夹下,然后通过程序解压。
当然这是一个鸡肋的方法,因为你可以直接下载csv文件,然而对于这样的一个问题,我还是过于偏执的下载了tgz文件并通过tarfile进行读取。

解决方案二

你也可以在工具下载时对该方法进行一些处理,确保下载的文件正确且完整。

源代码

# 下载数据
import os
import tarfile
import urllib.request
import requests

download_link = "https://github.com/ageron/handson-ml2/tree/master/"
housing_path = os.path.join("datasets", "housing")
housing_url = download_link + "datasets/housing/housing.tgz"


def download_source(url, output_path):
    response = requests.get(url, stream=False)
    with open(output_path, mode='wb') as f:
        f.write(response.content)
        

def fetch_housing_data(housing_url=housing_url, housing_path=housing_path):
    os.makedirs(housing_path, exist_ok=True)
    tgz_path = os.path.join(housing_path, "housing.tgz")
    if not os.path.exists(tgz_path):
        download_source(housing_url, tgz_path) # 使用requests.get方法
#         urllib.request.urlretrieve(housing_url, tgz_path) # 使用urllib.request.urlretrieve方法

    housing_tgz = tarfile.open(tgz_path, 'r:gz')
    housing_tgz.extractall(path=housing_path)
    housing_tgz.close()
    
fetch_housing_data()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
[/public/home/pengjy/anaconda3] >>> PREFIX=/public/home/pengjy/anaconda3 WARNING: md5sum mismatch of tar archive expected: 8a581514493c9e0a1cbd425bc1c7dd90 got: 614f6284c34f91affd38a1be2e4be076 - Unpacking payload ... Traceback (most recent call last): File "entry_point.py", line 76, in <module> File "tarfile.py", line 2024, in extractall File "tarfile.py", line 2065, in extract File "tarfile.py", line 2137, in _extract_member File "tarfile.py", line 2186, in makefile File "tarfile.py", line 249, in copyfileobj tarfile.ReadError: unexpected end of data [210095] Failed to execute script entry_point concurrent.futures.process._RemoteTraceback: ''' Traceback (most recent call last): File "concurrent/futures/process.py", line 368, in _queue_management_worker File "multiprocessing/connection.py", line 251, in recv TypeError: __init__() missing 1 required positional argument: 'msg' ''' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "entry_point.py", line 69, in <module> File "concurrent/futures/process.py", line 484, in _chain_from_iterable_of_lists File "concurrent/futures/_base.py", line 611, in result_iterator File "concurrent/futures/_base.py", line 439, in result File "concurrent/futures/_base.py", line 388, in __get_result concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending. [210105] Failed to execute script entry_point 是什么问题,如何解决?
07-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值