EOFError: Compressed file ended before the end-of-stream marker was reached

2,883 阅读1分钟

在使用 python 下载文件时,比如使用 MNIST 图片训练集,由于网络原因,可能造成临时文件没有下载成功,导致再次执行程序报错。如:

➜  cnn python3 cnn.py
Traceback (most recent call last):
  File "cnn.py", line 9, in <module>
    train_images = mnist.train_images()[:1000]
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mnist/__init__.py", line 161, in train_images
    return download_and_parse_mnist_file('train-images-idx3-ubyte.gz')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mnist/__init__.py", line 146, in download_and_parse_mnist_file
    return parse_idx(fd)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mnist/__init__.py", line 111, in parse_idx
    data = array.array(data_type, fd.read())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/gzip.py", line 276, in read
    return self._buffer.read(size)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/gzip.py", line 482, in read
    raise EOFError("Compressed file ended before the "
EOFError: Compressed file ended before the end-of-stream marker was reached

一般来说,解决方法都是删除临时文件,有一些解决方案给出的是删除 ~/.keras/datasets/ 目录下的 database 文件,如 这篇 StackOverFlow 给出的回答

而我在使用 MNIST 的手写图片时也碰到了这个问题,但 ~/.keras/datasets/ 目录下并没有内容。

实际上,文件下载到了临时文件目录,在临时文件目录下删除即可。

首先找到临时文件目录:

echo $TMPDIR

输出的目录即为临时文件目录,然后删除该目录下的临时文件即可。在本例子中是 train-images-idx3-ubyte.gz 文件等。