我无法从存档中提取 Python 3。调查的一些结果(在 Mac OS X 上)可能会有所帮助。
检查档案的健康状况
将文件设为只读以防止意外更改:
$ chmod -w vertnet_latest_amphibians.zip
$ ls -lh vertnet_latest_amphibians.zip
-r--r--r-- 1 lawh 2045336417 296M Jan 6 10:10 vertnet_latest_amphibians.zip
使用zip 和unzip 检查存档:
$ zip -T vertnet_latest_amphibians.zip
test of vertnet_latest_amphibians.zip OK
$ unzip -t vertnet_latest_amphibians.zip
Archive: vertnet_latest_amphibians.zip
testing: VertNet_Amphibia_eml.xml OK
testing: __MACOSX/ OK
testing: __MACOSX/._VertNet_Amphibia_eml.xml OK
testing: vertnet_latest_amphibians.csv OK
testing: __MACOSX/._vertnet_latest_amphibians.csv OK
No errors detected in compressed data of vertnet_latest_amphibians.zip
@sam-mussmann 也发现,7z 报告了 CRC 错误:
$ 7z t vertnet_latest_amphibians.zip
7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,4 CPUs x64)
Scanning the drive for archives:
1 file, 309726398 bytes (296 MiB)
Testing archive: vertnet_latest_amphibians.zip
--
Path = vertnet_latest_amphibians.zip
Type = zip
Physical Size = 309726398
ERROR: CRC Failed : vertnet_latest_amphibians.csv
Sub items Errors: 1
Archives with Errors: 1
Sub items Errors: 1
我的zip 和unzip 都比较老了; 7z 很新:
$ zip -v | head -2
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
This is Zip 3.0 (July 5th 2008), by Info-ZIP.
$ unzip -v | head -1
UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.
$ 7z --help |head -3
7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,4 CPUs x64)
提取
使用unzip:
$ time unzip vertnet_latest_amphibians.zip vertnet_latest_amphibians.csv
Archive: vertnet_latest_amphibians.zip
inflating: vertnet_latest_amphibians.csv
real 0m17.201s
user 0m14.281s
sys 0m2.460s
使用 Python 2.7.13 进行提取,为简洁起见,使用 zipfile 的命令行界面:
$ time ~/local/python-2.7.13/bin/python2 -m zipfile -e vertnet_latest_amphibians.zip .
real 0m19.491s
user 0m12.996s
sys 0m5.897s
如您所见,Python 3.6.0(也是 3.4.5 和 3.5.2)报告了错误的 CRC
假设 1:存档包含错误的 CRC,即 zip、unzip 和
Python 2.7.13 未能检测到; 7z 和 Python 3.4-3.6 都在做
正确的事情。
假设2:存档没问题; 7z 和 Python 3.4-3.6 都包含一个错误。
鉴于这些工具的相对年龄,我猜 H1 是正确的。
解决方法
如果您不使用 Windows 并且信任存档的内容,则使用常规 shell 命令可能更直接。比如:
wget <the-long-url> -O /tmp/vertnet_latest_amphibians.zip
unzip /tmp/vertnet_latest_amphibians.zip vertnet_latest_amphibians.csv
rm -rf /tmp/vertnet_latest_amphibians.zip
或者您可以在 Python 中执行 unzip:
import os
os.system('unzip vertnet_latest_amphibians.zip vertnet_latest_amphibians.csv')
偶然
捕捉ImportError 比检查版本稍微简洁一些
Python 解释器:
try:
from urllib.request import urlretrieve
except ImportError:
from urllib import urlretrieve