Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

simple UnZip

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


jenn.duerr at gmail

Jul 2, 2008, 6:39 AM

Post #1 of 6 (55 views)
Permalink
simple UnZip

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!
--
http://mail.python.org/mailman/listinfo/python-list


ojeeves at gmail

Jul 2, 2008, 7:01 AM

Post #2 of 6 (55 views)
Permalink
Re: simple UnZip [In reply to]

On Jul 2, 2:39 pm, noydb <jenn.du...@gmail.com> 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.
--
http://mail.python.org/mailman/listinfo/python-list


cwitts at gmail

Jul 2, 2008, 7:03 AM

Post #3 of 6 (55 views)
Permalink
Re: simple UnZip [In reply to]

On Jul 2, 3:39 pm, noydb <jenn.du...@gmail.com> 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!

Well, how is the output directory different ? Is it just a timestamp
like 'YYYYMMDD' that obviously changes every day or is it named after
the file used ? If it is timestamp style you can do

import zipfile, os, sys, time

folder_name = time.strftime("%Y%m%d", time.localtime(time.time()))
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(os.path.join(folder_name, zItem),'wb')
z1File.write(zRead)
z1File.close
print "Finished"

That will create a folder in your current working directory with the
name of todays date and unzip it there. This is just one example
route.
--
http://mail.python.org/mailman/listinfo/python-list


omer at no-log

Jul 2, 2008, 7:07 AM

Post #4 of 6 (55 views)
Permalink
Re: simple UnZip [In reply to]

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

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
--
http://mail.python.org/mailman/listinfo/python-list


jenn.duerr at gmail

Jul 2, 2008, 7:18 AM

Post #5 of 6 (54 views)
Permalink
Re: simple UnZip [In reply to]

On Jul 2, 10:07 am, Cédric Lucantis <o...@no-log.org> 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
>
> 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- Hide quoted text -
>
> - Show quoted text -

Thanks everyone! I did read the help on zipfile. I know it was
simple, but for newbies, sometimes it just helps to see how it is done
and learn from that.
--
http://mail.python.org/mailman/listinfo/python-list


google at mrabarnett

Jul 2, 2008, 8:25 AM

Post #6 of 6 (48 views)
Permalink
Re: simple UnZip [In reply to]

On Jul 2, 3:07 pm, Cédric Lucantis <o...@no-log.org> 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

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.