i wanna check a zip file in a directory.how can do it?
directory="c:\myzipfiles"
listfiles="a.zip,b.zip,c.txt,d.xls"
#a.zip is badfile

import os
import zipfile
mylist=os.listdir("c:\myzipfiles")
for zfile in mylist
if zipfile.ZipFile.test(zfile):
print"you can extract ",zfile
else:
print "you cant extract ",zfile

and how can i use zipfile.ZipFile.testzip()

thanks for answers...

testzip is to check the integrity of the files within the zip. To check if a zip itself is corrupt, pass it through a try statment.

import os, zipfile
mylist = os.listdir("C:\myzipfiles")
for z in mylist:
  try:
    x = zipfile.ZipFile(z)
    print "%s opened ok" % z
    x.close()
  except:
    print "File %s is corrupt..." % z
    continue

I made a script once that extracted over 30,000 zipfiles. It took 1.35 minuets.
The zips where ROMS for GBA, NES, SNES, and SEGA, each individually zipped.

testzip is to check the integrity of the files within the zip. To check if a zip itself is corrupt, pass it through a try statment.

import os, zipfile
mylist = os.listdir("C:\myzipfiles")

Little less stupid listing

import os, zipfile
mylist = [zip for zip in os.listdir("C:\myzipfiles") if zip.endswith('.zip')

thank you for answers. i used tech B code for this file. why it gave "... is corrupt..."

1.200904011.zip is a zip file and i can extract it with winrar.
2.200904052.zip is broken zip file and i cant extract it with winrar

tonyjv code can says files extensions zip or not zip.

thanks

Attachments
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>Sayfa bulunamad - MKB</TITLE>
<META http-equiv=content-type content="text/html; charset=UTF-8" />
<META content="" name=description />
<LINK media=screen href="/images/error.css" 
type=text/css rel=stylesheet />
<META content="MSHTML 6.00.6001.18248" name=GENERATOR /></HEAD>
<BODY>
<DIV id=mainContainer>
<DIV class=topLeft></DIV>
<DIV class=topRight></DIV>
<DIV class=bottomLeft></DIV>
<DIV class=bottomRight></DIV>
<H1 style="text-align:center"><A title="Ana sayfaya geri dn..." 
href="http://www.imkb.gov.tr/"><SPAN>#</SPAN><img src="/images/404.png" width="128" height="128" border="0"></A></H1>
<DIV id=message>


<H2>Grntlemek istediiniz sayfada hata olutu.</H2>
<p align="left">Bu hatann sebebi aadakilerden biri olabilir:</p>
			<ul>
			<li><p align="left">Grntlemek istediiniz sayfa tand</p></li>
			<li><p align="left">Adres yanl girildi</p></li>
			<li><p align="left">Bu sayfay grmeye yetkiniz yok</p></li>
			<li><p align="left">Veritaban ya da sunucu balantsnda bir hata var</p></li>
			<li><p align="left">Kullanc tarafndan yanl bir ilem yapld, ya da </p></li>
			<li><p align="left">Uygulama seviyesinde genel bir hata olutu</p></li>
			</ul>
<P><A title="Ana sayfa" href="http://www.imkb.gov.tr/"> Ana sayfaya dnmek iin tklaynz.</A></P></DIV>
<DIV class=clear></DIV></DIV>

<DIV id=mainContainer>
<DIV class=topLeft></DIV>
<DIV class=topRight></DIV>
<DIV class=bottomLeft></DIV>
<DIV class=bottomRight></DIV>
<H1 style="text-align:center"><A title="Ana sayfaya geri dn..." 
href="http://www.imkb.gov.tr/"><SPAN>#</SPAN><img src="/images/404.png" width="128" height="128" border="0"></A></H1>
<DIV id=message>


<H2>An error occurred during the current web request.</H2>
<p align="left">Possible reasons for this error:</p>
			<ul>
			<li><p align="left">The page you requested has been moved</p></li>
			<li><p align="left">An invalid web address was entered</p></li>
			<li><p align="left">You do not have permission to view this page</p></li>
			<li><p align="left">There is a problem with the database or web server</p></li>
			<li><p align="left">An invalid operation was attempted by the user, or </p></li>
			<li><p align="left">A general error occurred at the application level.</p></li>
			</ul>
<P><A title="Home page" href="http://www.ise.org/"> Click here to return to the main page.</A></P></DIV>
<DIV class=clear></DIV></DIV>

</BODY></HTML>

It appears I left out closing bracket while typing with my mobile the previous answer. Also the name of variable in list comprehension is zip, which is reserved for function zip, so I changed it to zipf.

Yes. 200904052.zip looks broken and it can not be opened by the program or 7zip program.

Here is program, which compares the effect of my modification for output compared to original full listdir. I do nt think it is nice to get announcement your program is corrupt, ilke here when mylist is full directory. That is why I called my program little less stupid.

import os, zipfile
for mylist in ([zipf for zipf in os.listdir(os.curdir) if zipf.endswith('.zip')],os.listdir(os.curdir)):
    for z in mylist:
      try:
        x = zipfile.ZipFile(z)
        print "%s opened ok" % z
        x.close()
      except:
        print "File %s is corrupt..." % z
        continue
    print '-'*40
""" Example output:
>>> 
200904011.zip opened ok
File 200904052.zip is corrupt...
8bf_tools.zip opened ok
ClientForm-0.2.10.zip opened ok
code.zip opened ok
LiveWires 2.0.zip opened ok
livewires-2.1-r2(2).zip opened ok
livewires-2.1-r2.zip opened ok
myro-all-270.zip opened ok
pyke-1.1.zip opened ok
pySTEP_1.20.zip opened ok
SimplistiCalc.zip opened ok
testdatadaniweb.zip opened ok
----------------------------------------
End of output from original version (all files):
.....
File setup-unicon.exe is corrupt...
File ship.bmp is corrupt...
File SimplistiCalc is corrupt...
SimplistiCalc.zip opened ok
testdatadaniweb.zip opened ok
File testzip.py is corrupt...
File Thumbs.db is corrupt...

"""

Edited 6 Years Ago by pyTony: n/a

thats work perfect... thanks for answers tonyjv...

This question has already been answered. Start a new discussion instead.