Re: simple UnZip



On Jul 2, 2:39 pm, noydb <jenn.du...@xxxxxxxxx> wrote:
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
print "Finished"
&&&

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

The scenario is that the zip file will always be the same (gets copied
over daily), but it needs to be unzipped to a specific different
directory.

Can anyone help?

Thanks!

Firstly, I'd recommend just reading the documentation for the zipfile
module, it's fairly straight forwards.

To write the files to a different location, just change this line:

z1File = open(zItem,'wb')

This is where you're opening a file to write to.

Try something like:

z1File = open( os.path.join(output_dir, zItem), 'wb')

to write the files into the path specified in output_dir.

You don't even have to save the files with the same names they have in
the zipfile; you don't have to save the files at all. In one project
I'm working on, I just read files from a zip file into memory and
process them there.
.



Relevant Pages

  • 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: 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)
  • simple UnZip
    ... Can someone help me with this script, ... unzips one file from a zipfile, not all files in a zipfile. ... for fzip in zips: ... print fzip," is a zip" ...
    (comp.lang.python)
  • Re: simple UnZip
    ... unzips one file from a zipfile, not all files in a zipfile. ... for fzip in zips: ... print fzip," is a zip" ... for zItem in zList: ...
    (comp.lang.python)
  • Re: Zippping a directory recursively from Python script on Windows
    ... > but here's a simple example of using ZipFile for creating zips of ... > compressing those most likely, but the basics are there. ...
    (comp.lang.python)