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')

Why was my way stupid?

Oh and you have a syntax error; didn't close the list.

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

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...

"""

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.