簡體   English   中英

在 Mac os X 上使用 tarfile 打開 .tgz 文件

[英]Opening .tgz files with tarfile on Mac os X

我正在使用 Scikit-Learn 和 TensorFlow 學習這本書,在第一個項目中,您可能知道,它將處理住房數據集。

當我想用tarfile打開housing.tgz時,我收到錯誤: ReadError: file could not be opened successfully.

我是 Mac OS 用戶,我搜索了很多。 但我無論如何都找不到處理這個問題的方法。 即使我嘗試了 [this question][1] 中的解決方案,但它沒有奏效。 似乎 Mac 無法打開 .tgz 文件。 有人可以幫忙嗎?(我從書中復制了代碼,代碼沒有問題)

提前致謝!

我的代碼

import os
import tarfile
from six.moves import urllib

DOWNLOAD_ROOT = "https://github.com/ageron/handson-ml2/tree/master/"
HOUSING_PATH = os.path.join("datasets", "housing")
HOUSING_URL = DOWNLOAD_ROOT + "datasets/housing/housing.tgz"
def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):
    if not os.path.isdir(housing_path):
        os.makedirs(housing_path)
    # os.makedirs(housing_path, exist_ok=True)
    tgz_path = os.path.join(housing_path, "housing.tgz")
    urllib.request.urlretrieve(housing_url, tgz_path)
    housing_tgz = tarfile.open(tgz_path)
    housing_tgz.extractall(path=housing_path)
    housing_tgz.close()
fetch_housing_data()```


  [1]: https://stackoverflow.com/questions/46651490/tarfile-cant-open-tgz

您用於下載存檔的 URL 似乎不正確; 您正在下載 HTML 文件,因此出現錯誤。 正確的 URL 將是: https://raw.githubusercontent.com/ageron/handson-ml2/master/datasets/housing/housing.tgz - 注意域。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM