Python 3.8.3 "File-not-found" 消息
Python 3.8.3 "File-not-found" message
我目前正在学习《Hands-On Machine Learning with Scikit-Learn, Keras and TensorFlow》一书。我尝试了 运行 以下示例,但没有成功。 link 正常工作,pandas 安装正确,os,tarfile 和 urllib 是系统包。尽管如此,我还是收到以下错误消息(尝试过 Jupyter 和 Spyder):
import os
import tarfile
import urllib
import pandas as pd
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()
def load_housing_data(housing_path = HOUSING_PATH):
csv_path = os.path.join(housing_path, "housing.csv")
return pd.read_csv(csv_path)
housing = load_housing_data()
print(housing)
Jupyter 中的错误消息:
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call
last) in
21 return pd.read_csv(csv_path)
22
---> 23 housing = load_housing_data()
24 housing.head()
in load_housing_data(housing_path)
19 def load_housing_data(housing_path = HOUSING_PATH):
20 csv_path = os.path.join(housing_path, "housing.csv")
---> 21 return pd.read_csv(csv_path)
22
23 housing = load_housing_data()
~\Miniconda3\lib\site-packages\pandas\io\parsers.py in
read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col,
usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters,
true_values, false_values, skipinitialspace, skiprows, skipfooter,
nrows, na_values, keep_default_na, na_filter, verbose,
skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col,
date_parser, dayfirst, cache_dates, iterator, chunksize, compression,
thousands, decimal, lineterminator, quotechar, quoting, doublequote,
escapechar, comment, encoding, dialect, error_bad_lines,
warn_bad_lines, delim_whitespace, low_memory, memory_map,
float_precision)
684 )
685
--> 686 return _read(filepath_or_buffer, kwds)
687
688
~\Miniconda3\lib\site-packages\pandas\io\parsers.py in
_read(filepath_or_buffer, kwds)
450
451 # Create the parser.
--> 452 parser = TextFileReader(fp_or_buf, **kwds)
453
454 if chunksize or iterator:
~\Miniconda3\lib\site-packages\pandas\io\parsers.py in init(self,
f, engine, **kwds)
934 self.options["has_index_names"] = kwds["has_index_names"]
935
--> 936 self._make_engine(self.engine)
937
938 def close(self):
~\Miniconda3\lib\site-packages\pandas\io\parsers.py in
_make_engine(self, engine) 1166 def _make_engine(self, engine="c"): 1167 if engine == "c":
-> 1168 self._engine = CParserWrapper(self.f, **self.options) 1169 else: 1170 if engine == "python":
~\Miniconda3\lib\site-packages\pandas\io\parsers.py in init(self,
src, **kwds) 1996 kwds["usecols"] = self.usecols 1997
-> 1998 self._reader = parsers.TextReader(src, **kwds) 1999 self.unnamed_cols = self._reader.unnamed_cols 2000
pandas_libs\parsers.pyx in
pandas._libs.parsers.TextReader.cinit()
pandas_libs\parsers.pyx in
pandas._libs.parsers.TextReader._setup_parser_source()
FileNotFoundError: [Errno 2] No such file or directory:
'datasets\housing\housing.csv'
如果有人花时间 reproduce/provide 输入代码 returns 是否是错误消息,我将不胜感激。
非常感谢!
本地文件"datasets/housing/housing.csv"
只在调用
时创建
fetch_housing_data()
您的代码示例没有调用此函数。尝试在 housing = load_housing_data()
.
之前添加此行
我目前正在学习《Hands-On Machine Learning with Scikit-Learn, Keras and TensorFlow》一书。我尝试了 运行 以下示例,但没有成功。 link 正常工作,pandas 安装正确,os,tarfile 和 urllib 是系统包。尽管如此,我还是收到以下错误消息(尝试过 Jupyter 和 Spyder):
import os
import tarfile
import urllib
import pandas as pd
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()
def load_housing_data(housing_path = HOUSING_PATH):
csv_path = os.path.join(housing_path, "housing.csv")
return pd.read_csv(csv_path)
housing = load_housing_data()
print(housing)
Jupyter 中的错误消息:
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) in 21 return pd.read_csv(csv_path) 22 ---> 23 housing = load_housing_data() 24 housing.head()
in load_housing_data(housing_path) 19 def load_housing_data(housing_path = HOUSING_PATH): 20 csv_path = os.path.join(housing_path, "housing.csv") ---> 21 return pd.read_csv(csv_path) 22 23 housing = load_housing_data()
~\Miniconda3\lib\site-packages\pandas\io\parsers.py in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision) 684 ) 685 --> 686 return _read(filepath_or_buffer, kwds) 687 688
~\Miniconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds) 450 451 # Create the parser. --> 452 parser = TextFileReader(fp_or_buf, **kwds) 453 454 if chunksize or iterator:
~\Miniconda3\lib\site-packages\pandas\io\parsers.py in init(self, f, engine, **kwds) 934 self.options["has_index_names"] = kwds["has_index_names"] 935 --> 936 self._make_engine(self.engine) 937 938 def close(self):
~\Miniconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine) 1166 def _make_engine(self, engine="c"): 1167 if engine == "c": -> 1168 self._engine = CParserWrapper(self.f, **self.options) 1169 else: 1170 if engine == "python":
~\Miniconda3\lib\site-packages\pandas\io\parsers.py in init(self, src, **kwds) 1996 kwds["usecols"] = self.usecols 1997 -> 1998 self._reader = parsers.TextReader(src, **kwds) 1999 self.unnamed_cols = self._reader.unnamed_cols 2000
pandas_libs\parsers.pyx in pandas._libs.parsers.TextReader.cinit()
pandas_libs\parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()
FileNotFoundError: [Errno 2] No such file or directory: 'datasets\housing\housing.csv'
如果有人花时间 reproduce/provide 输入代码 returns 是否是错误消息,我将不胜感激。
非常感谢!
本地文件"datasets/housing/housing.csv"
只在调用
fetch_housing_data()
您的代码示例没有调用此函数。尝试在 housing = load_housing_data()
.