【发布时间】:2020-02-10 22:00:50
【问题描述】:
我正在使用 Aurelien Geron 的“Hands-on machine learning with scikit-learn and tensorflow”一书。
这是我第一次使用 Jupyter 和 Python。
我的问题是当我使用以下代码运行单元时:
import os
import tarfile
import urllib
DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml2/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):
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()
单元格评估永远不会结束,In[*]: 永远不会变成In[1]: 之类的东西。
所以,我认为这是初始 url 的问题,因为当我通过 Internet 浏览器访问它时显示错误。
因此,我将其更改为DOWNLOAD_ROOT = "https://github.com/ageron/handson-ml2/tree/master/"。
现在我得到In[1]:。但是,当我运行 fetch_housing_data() 时,我得到:
---------------------------------------------------------------------------
ReadError Traceback (most recent call last)
<ipython-input-6-bd66b1fe6daf> in <module>
----> 1 fetch_housing_data()
<ipython-input-5-ef3c39b342d8> in fetch_housing_data(housing_url, housing_path)
9 tgz_path = os.path.join(housing_path, "housing.tgz")
10 urllib.request.urlretrieve(housing_url, tgz_path)
---> 11 housing_tgz = tarfile.open(tgz_path)
12 housing_tgz.extractall(path=housing_path)
13 housing_tgz.close()
~\Anaconda3\lib\tarfile.py in open(cls, name, mode, fileobj, bufsize, **kwargs)
1576 fileobj.seek(saved_pos)
1577 continue
-> 1578 raise ReadError("file could not be opened successfully")
1579
1580 elif ":" in mode:
ReadError: file could not be opened successfully
为什么会发生这种情况,我该如何解决?
【问题讨论】:
标签: python import jupyter-notebook