simple UnZip
Chris
cwitts at gmail.com
Wed Jul 2 10:03:56 EDT 2008
On Jul 2, 3:39 pm, noydb <jenn.du... at 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.
More information about the Python-list
mailing list