用于检查 zip 文件是否损坏的 Python 脚本 [英] Python script to check if a zip file is corrupt
问题描述
我如何检查 zip 文件是否已损坏....就像我有一个包含 10 张 jpg 图像的 zip 文件,我可以提取其中的 8 张图像,其中 zip 中的两个图像已损坏,我我无法提取有没有办法在 python 脚本中检查这个
How do i check if a zip file is corrupt or not.... like i have a zip file with 10 jpg images i am able to extract say 8 of the images two of the images in the zip are corrupt and i am not able to extract is there a way to check this in a python script
推荐答案
此代码将抛出异常(如果 zip 文件确实很糟糕或不是 zip 文件),或者显示 zip 中的第一个错误文件文件.
This code will either throw an exception (if the zip file is really bad or if it's not a zip file), or show the first bad file in the zip file.
import os
import sys
import zipfile
if __name__ == "__main__":
args = sys.argv[1:]
print "Testing zip file: %s" % args[0]
the_zip_file = zipfile.ZipFile(args[0])
ret = the_zip_file.testzip()
if ret is not None:
print "First bad file in zip: %s" % ret
sys.exit(1)
else:
print "Zip file is good."
sys.exit(0)
当然,您应该将这些内容包含在适当的 try/except 子句中.但这是基础.
You should, of course, enclose this stuff in proper try/except clauses. But that's the basics.
这篇关于用于检查 zip 文件是否损坏的 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!