Newbie requires help me on how to use the ZipFile module please.

I

Ian Cook

Hi,
Can someone PLEASE shed some light on using the ZipFile module
correctly.
I'm new to python so please excuse me if this is a 'dumb' question.

If I use mode='a' then what happens is the zip file just gets bigger
and bigger as the SAME file is appended to the zipfile.

If I use mode='w' then what happens is the zip file is erased before
adding files to it. Therefore I lose the files that were in the
original file zipfile.

What I want is to use the ZipFile module to UPDATE an existing zip
file, rather than append, or create a new zip file. The zip file then
contains my original files and any updated files.

Does anyone know how this can be done with the Zipfile module?

This is a snippet of the code I have now..

try:
zf = zipfile.ZipFile(zfname,mode='a')
except:
zf = zipfile.ZipFile(zfname,mode='w')

for name in cutflist:
fp = os.path.join(root,name)
print "adding ",fp,"...",
compresstype = zipfile.ZIP_DEFLATED
if zipfile.is_zipfile(fp):
compresstype = zipfile.ZIP_STORED
zf.write(fp,compress_type=compresstype)
print 'done.'



Thanks in advance.

Ian Cook
(Freeware author of Kirby Alarm And Task Scheduler)
http://www.kirbyfooty.com/
 
D

David Fraser

Ian said:
Hi,
Can someone PLEASE shed some light on using the ZipFile module
correctly.
I'm new to python so please excuse me if this is a 'dumb' question.

If I use mode='a' then what happens is the zip file just gets bigger
and bigger as the SAME file is appended to the zipfile.

If I use mode='w' then what happens is the zip file is erased before
adding files to it. Therefore I lose the files that were in the
original file zipfile.

What I want is to use the ZipFile module to UPDATE an existing zip
file, rather than append, or create a new zip file. The zip file then
contains my original files and any updated files.

Does anyone know how this can be done with the Zipfile module?

This is a snippet of the code I have now..

try:
zf = zipfile.ZipFile(zfname,mode='a')
except:
zf = zipfile.ZipFile(zfname,mode='w')

for name in cutflist:
fp = os.path.join(root,name)
print "adding ",fp,"...",
compresstype = zipfile.ZIP_DEFLATED
if zipfile.is_zipfile(fp):
compresstype = zipfile.ZIP_STORED
zf.write(fp,compress_type=compresstype)
print 'done.'



Thanks in advance.

Ian Cook

Curious, I posted a reply to the similar question you posted a week or
two ago, but it doesn't show up in google groups... even though I got it
from my nntp server...
So I'll post it again and Cc you :)
Instead of attaching the file, you can get it here:
http://cvs.sourceforge.net/viewcvs.py/*checkout*/translate/src/translate/misc/zipfileext.py?rev=1.3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top