d2l-ai/d2l-zh · error · AssertionError
只有zip/tar文件可以被解压缩
Error message
只有zip/tar文件可以被解压缩
What it means
An AssertionError (assert False) in d2l.tensorflow.download_extract that fires when the downloaded file's extension is neither .zip nor .tar/.gz. The function only knows how to open archives via zipfile.ZipFile or tarfile.open; any other extension aborts with the Chinese message 'only zip/tar files can be extracted'.
Source
Thrown at d2l/tensorflow.py:395
print(f'正在从{url}下载{fname}...')
r = requests.get(url, stream=True, verify=True)
with open(fname, 'wb') as f:
f.write(r.content)
return fname
def download_extract(name, folder=None):
"""下载并解压zip/tar文件
Defined in :numref:`sec_kaggle_house`"""
fname = download(name)
base_dir = os.path.dirname(fname)
data_dir, ext = os.path.splitext(fname)
if ext == '.zip':
fp = zipfile.ZipFile(fname, 'r')
elif ext in ('.tar', '.gz'):
fp = tarfile.open(fname, 'r')
else:
assert False, '只有zip/tar文件可以被解压缩'
fp.extractall(base_dir)
return os.path.join(base_dir, folder) if folder else data_dir
def download_all():
"""下载DATA_HUB中的所有文件
Defined in :numref:`sec_kaggle_house`"""
for name in DATA_HUB:
download(name)
DATA_HUB['kaggle_house_train'] = (
DATA_URL + 'kaggle_house_pred_train.csv',
'585e9cc93e70b39160e7921475f9bcd7d31219ce')
DATA_HUB['kaggle_house_test'] = (
DATA_URL + 'kaggle_house_pred_test.csv',
'fa19780a7b011d9b009e8bff8e99922a8ee2eb90')
View on GitHub (pinned to e6b18ccea7)
Solutions
- For plain files (csv/json/txt), call d2l.download(name) instead of download_extract(name) — no extraction is needed.
- For .bz2/.xz archives, pre-extract manually (tar xjf file.tar.bz2) or register a .zip/.tar.gz mirror in DATA_HUB.
- Fix the registered URL so it ends with a real archive extension before calling download_extract.
- For .tar.bz2 specifically, note os.path.splitext returns '.bz2'; rename or handle with tarfile.open(fname, 'r:bz2') in your own code.
Example fix
# before
d2l.DATA_HUB['raw_csv'] = (d2l.DATA_URL + 'data.csv', sha1)
fname = d2l.download_extract('raw_csv') # AssertionError: csv is not zip/tar
# after
fname = d2l.download('raw_csv') # plain download, no extraction Defensive patterns
Strategy: validation
Validate before calling
import os
_, ext = os.path.splitext(fname)
if ext in ('.zip', '.tar', '.gz'):
out = d2l.download_extract(name)
elif ext in ('.csv', '.json', '.txt'):
out = d2l.download(name)
else:
raise ValueError(f'unsupported extension {ext!r}; extract {fname} manually') Type guard
def is_extractable(fname: str) -> bool:
return os.path.splitext(fname)[1] in ('.zip', '.tar', '.gz') Try / catch
try:
d2l.download_extract(name)
except AssertionError as e:
if 'zip/tar' in str(e):
path = d2l.download(name) # plain file, no extraction needed
else:
raise Prevention
- Use download() for plain files and download_extract() only for .zip/.tar/.gz.
- Remember splitext('a.tar.bz2') -> '.bz2' is rejected; pre-extract such archives yourself.
- Register DATA_HUB URLs that end in a real, recognizable extension.
When it happens
Trigger: Registering a DATA_HUB entry whose URL ends in .csv, .json, .txt, .pt, or .7z and then calling download_extract on it; a URL with no filename extension at all (query-string URLs); double-extension files where os.path.splitext picks up the wrong part (e.g. 'data.tar.bz2' yields '.bz2').
Common situations: Users pointing DATA_HUB at raw CSVs (correct target is download(), not download_extract()); .bz2 or .xz compressed archives from non-D2L sources; URLs like https://host/file?format=zip whose splitext result is ''.
Related errors
- 只有zip/tar文件可以被解压缩
- 只有zip/tar文件可以被解压缩
- 只有zip/tar文件可以被解压缩
- f"{name} 不存在于 {DATA_HUB}"
- f"{name} 不存在于 {DATA_HUB}"
AI-assisted analysis of d2l-ai/d2l-zh@e6b18ccea7 (2026-08-14).
Data as JSON: /api/errors/cd2bfaab88d697ce.
Report an issue: GitHub.