Re: simple UnZip



On Jul 2, 3:07 pm, Cédric Lucantis <o...@xxxxxxxxxx> wrote:
Le Wednesday 02 July 2008 15:39:51 noydb, vous avez écrit :



Can someone help me with this script, which I found posted elsewhere?
I'm trying to figure out what is going on here so that I can alter it
for my needs, but the lack of descriptive names is making it
difficult.  And, the script doesn't quite do anything worthwhile -- it
unzips one file from a zipfile, not all files in a zipfile.

***
import zipfile, os, sys, glob

os.chdir("C:\\Temp")
zips = glob.glob('*.zip')

for fzip in zips:
    if zipfile.is_zipfile(fzip):
        print fzip," is a zip"
        z = zipfile.ZipFile(fzip,'r')
        lstName = z.namelist()
        sHgt = lstName[0]
        print "Unpacking",sHgt
        hgt = z.read(sHgt)
        fHgt = open(sHgt,'wb')
        fHgt.write(hgt)
        # fHgt.flush
        fHgt.close
print "Finished"
***

I changed it somewhat to
&&&
import zipfile, os, sys

event_zip = ("C:\\Temp\\data4event.zip")

z = zipfile.ZipFile(event_zip, 'r')

zList = z.namelist()

for zItem in zList:
    print "Unpacking",zItem
    zRead = z.read(zItem)
    z1File = open(zItem,'wb')
    z1File.write(zRead)
    z1File.close

It's not actually closing the file. It should be:

z1File.close()


namelist() returns a list of relative file names, so you can just put them
anywhere you want with:

zlFile = open(os.path.join(DESTDIR, zItem), 'wb')

or change the current directory, but the first way should be preferred.

print "Finished"
&&&

This works, but I want to be able to specify a different output
location.

--
Cédric Lucantis

.



Relevant Pages

  • Re: simple UnZip
    ... unzips one file from a zipfile, not all files in a zipfile. ... import zipfile, os, sys, glob ... how is the output directory different? ... If it is timestamp style you can do ...
    (comp.lang.python)
  • Re: Archiving directory without external tools?
    ... I'd like to avoid reliance on extenal tools ... import os, sys, zipfile ... for path, dirs, files in os.walk: ...
    (comp.lang.python)
  • Re: simple UnZip
    ... unzips one file from a zipfile, not all files in a zipfile. ... import zipfile, os, sys, glob ... for fzip in zips: ...
    (comp.lang.python)
  • Re: simple UnZip
    ... I'm trying to figure out what is going on here so that I can alter it ... unzips one file from a zipfile, not all files in a zipfile. ... import zipfile, os, sys, glob ...
    (comp.lang.python)
  • Re: BadZipfile "file is not a zip file"
    ... I am able to uncompress it in Windows XP, ... ZipFile can read table of contents: ...     Help on method printdir in module zipfile: ... The compression method used ...
    (comp.lang.python)