我的程序使用tar.gz
个文件列表。循环遍历该列表,每个元素获取:tar.open(elem, 'r:gz')
我遇到的问题是文件被截断时,循环将中断并且我将收到IOError : CRC check failed 0x5b5655a3 != 0x7ee38e85L.
我已经通过在文件名的命令行tar tvf
上运行来证明该文件已损坏/被截断。这将返回文件信息而不提取它。
-rw-rw-rw- 0 root root 3008744 Nov 13 05:06 alphabot_000573_2019_11_13_02_38_07-asynchronous.txt
-rw-rw-rw- 0 root root 53686251 Nov 12 21:42 alphabot_000573_2019_11_13_02_38_07-data.1.txt
-rw-rw-rw- 0 root root 29730477 Nov 12 21:44 alphabot_000573_2019_11_13_02_38_07-data.txt
-rw-rw-rw- 0 root root 36658 Nov 13 04:12 alphabot_000573_2019_11_13_02_38_07-nb-asynchronous.txt
-rw-rw-rw- 0 root root 81771 Nov 13 00:15 alphabot_000573_2019_11_13_02_38_07-synchronous.txt
-rw-rw-rw- 0 root root 169154963 Nov 13 17:34 alphabot_000574_2019_11_13_10_06_49-asynchronous.txt
tar: Truncated input file (needed 169155072 bytes, only 0 available)
tar: Error exit delayed from previous errors.
在我的python程序中,我想忽略已损坏/截断的tar.gz文件。我尝试修复此IOError的代码:
try:
tar = tarfile.open(elem, 'r:gz')
except IOError:
continue
但是该程序似乎忽略了这种期望,并且无法继续下一个元素的循环。
这是完整的代码块,可让您对我的程序有更广泛的了解
for elem in listOfFiles:
print elem
try:
tar = tarfile.open(elem, 'r:gz')
except IOError:
continue
for member in tar.getmembers():
f = tar.extractfile(member)