473,796 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:\\T emp")
zips = glob.glob('*.zi p')

for fzip in zips:
if zipfile.is_zipf ile(fzip):
print fzip," is a zip"
z = zipfile.ZipFile (fzip,'r')
lstName = z.namelist()
sHgt = lstName[0]
print "Unpacking",sHg t
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\\dat a4event.zip")

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

zList = z.namelist()

for zItem in zList:
print "Unpacking",zIt em
zRead = z.read(zItem)
z1File = open(zItem,'wb' )
z1File.write(zR ead)
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!
Jul 2 '08 #1
5 1431
oj
On Jul 2, 2:39*pm, noydb <jenn.du...@gma il.comwrote:
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:\\T emp")
zips = glob.glob('*.zi p')

for fzip in zips:
* * if zipfile.is_zipf ile(fzip):
* * * * print fzip," is a zip"
* * * * z = zipfile.ZipFile (fzip,'r')
* * * * lstName = z.namelist()
* * * * sHgt = lstName[0]
* * * * print "Unpacking",sHg t
* * * * 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\\dat a4event.zip")

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

zList = z.namelist()

for zItem in zList:
* * print "Unpacking",zIt em
* * zRead = z.read(zItem)
* * z1File = open(zItem,'wb' )
* * z1File.write(zR ead)
* * 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(ou tput_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.
Jul 2 '08 #2
On Jul 2, 3:39*pm, noydb <jenn.du...@gma il.comwrote:
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:\\T emp")
zips = glob.glob('*.zi p')

for fzip in zips:
* * if zipfile.is_zipf ile(fzip):
* * * * print fzip," is a zip"
* * * * z = zipfile.ZipFile (fzip,'r')
* * * * lstName = z.namelist()
* * * * sHgt = lstName[0]
* * * * print "Unpacking",sHg t
* * * * 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\\dat a4event.zip")

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

zList = z.namelist()

for zItem in zList:
* * print "Unpacking",zIt em
* * zRead = z.read(zItem)
* * z1File = open(zItem,'wb' )
* * z1File.write(zR ead)
* * 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\\dat a4event.zip")

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

zList = z.namelist()

for zItem in zList:
print "Unpacking",zIt em
zRead = z.read(zItem)
z1File = open(os.path.jo in(folder_name, zItem),'wb')
z1File.write(zR ead)
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.
Jul 2 '08 #3
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:\\T emp")
zips = glob.glob('*.zi p')

for fzip in zips:
if zipfile.is_zipf ile(fzip):
print fzip," is a zip"
z = zipfile.ZipFile (fzip,'r')
lstName = z.namelist()
sHgt = lstName[0]
print "Unpacking",sHg t
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\\dat a4event.zip")

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

zList = z.namelist()

for zItem in zList:
print "Unpacking",zIt em
zRead = z.read(zItem)
z1File = open(zItem,'wb' )
z1File.write(zR ead)
z1File.close
namelist() returns a list of relative file names, so you can just put them
anywhere you want with:

zlFile = open(os.path.jo in(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
Jul 2 '08 #4
On Jul 2, 10:07*am, Cédric Lucantis <o...@no-log.orgwrote:
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:\\T emp")
zips = glob.glob('*.zi p')
for fzip in zips:
* * if zipfile.is_zipf ile(fzip):
* * * * print fzip," is a zip"
* * * * z = zipfile.ZipFile (fzip,'r')
* * * * lstName = z.namelist()
* * * * sHgt = lstName[0]
* * * * print "Unpacking",sHg t
* * * * 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\\dat a4event.zip")
z = zipfile.ZipFile (event_zip, 'r')
zList = z.namelist()
for zItem in zList:
* * print "Unpacking",zIt em
* * zRead = z.read(zItem)
* * z1File = open(zItem,'wb' )
* * z1File.write(zR ead)
* * z1File.close

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

zlFile = open(os.path.jo in(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.
Jul 2 '08 #5
On Jul 2, 3:07*pm, Cédric Lucantis <o...@no-log.orgwrote:
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:\\T emp")
zips = glob.glob('*.zi p')
for fzip in zips:
* * if zipfile.is_zipf ile(fzip):
* * * * print fzip," is a zip"
* * * * z = zipfile.ZipFile (fzip,'r')
* * * * lstName = z.namelist()
* * * * sHgt = lstName[0]
* * * * print "Unpacking",sHg t
* * * * 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\\dat a4event.zip")
z = zipfile.ZipFile (event_zip, 'r')
zList = z.namelist()
for zItem in zList:
* * print "Unpacking",zIt em
* * zRead = z.read(zItem)
* * z1File = open(zItem,'wb' )
* * z1File.write(zR ead)
* * 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.jo in(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
Jul 2 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3377
by: Chris | last post by:
Hi Where can I find info on unzipping file with VB.NET. I need to unzip a winzip file with my application Thanks
1
1856
by: Jean Christophe Avard | last post by:
Hi! Finally I figure out what was wrong... "objZipEntry.size = strmFile" I wasn't giving the right file length... But now, I have issue with the unzip function... I can't unzip it, the unzip function return true but its invalid parameter when I try to set the image to the picture box.... Private Overloads Function Unzip(ByVal strSource As String, ByVal strFileToExtract As String, ByRef newms As MemoryStream) As Boolean If...
3
5787
by: SDRoy | last post by:
Hello Can someone tell me how I can unzip a .zip file in C#. The zip file is already there and I just need to unzip..not zip and unzip. -- Thanks, SDRoy
4
6028
by: DataSmash | last post by:
I need to unzip all zip file(s) in the current directory into their own subdirectories. The zip file name(s) always start with the string "usa" and end with ".zip". The code below will make the subdirectory, move the zip file into the subdirectory, but unzips the contents into the root (current) directory. I want the contents of the zip file unloaded into the newly created subdirectory where the zip file is. Any ideas? Thanks.
0
1679
by: Rocky Zhou | last post by:
python unzip At first, I tried to use 'os.popen3("unzip ...") like this: fin, fout, ferr = os.popen3("unzip -o -d %s %s" % (dest, zipfile)) strerr = ferr.read() # This makes the program hanging up if strerr: print >sys.stderr, strerr outlog.error(strerr)
5
7043
by: =?Utf-8?B?anVsaW8=?= | last post by:
Hi, I write a program to unzip a Tar file generated on a Unix environment file using SharpZipLib, but returns a error "Header checksum is invalid" when execute the program. This error appears when I try to extract the files. Can any help me where is a sample to unzip a tar file TIA Julio
3
2163
by: jld730 | last post by:
Hi All, I am looking for help on this simple script to unzip/extract the contents of a zip file. This is what I have so far: import zipfile, os, sys zip1 = ("C:\\Temp\\test11.zip") z = zipfile.ZipFile(zip1, 'r')
1
8535
by: olddocks | last post by:
I want to upload a zip file and then extract/unzip it. I am accomplishing this with php exec command. I am calling unzip from php exec command within a php script and it is not extracting files. why? <?php echo exec('unzip file.zip'); ?> i checked apache logs and it says It works perfectly fine when i unzip using SSH command line.. how to fix this problem.
2
6394
by: somsub | last post by:
Hi all, Here is my samle code use strict ; use warnings ; use IO::Uncompress::Unzip ; When I compiled this three lines of code in win32 I got error like below.
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.