在使用 python 下載文件時,好比使用 MNIST 圖片訓練集,因爲網絡緣由,可能形成臨時文件沒有下載成功,致使再次執行程序報錯。如:python
➜ 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 給出的回答。shell
而我在使用 MNIST 的手寫圖片時也碰到了這個問題,但 ~/.keras/datasets/
目錄下並無內容。bash
實際上,文件下載到了臨時文件目錄,在臨時文件目錄下刪除便可。網絡
首先找到臨時文件目錄:ui
echo $TMPDIR
複製代碼
輸出的目錄即爲臨時文件目錄,而後刪除該目錄下的臨時文件便可。在本例子中是 train-images-idx3-ubyte.gz
文件等。spa