Using zipfile.py

Mario Tola mtbros at tin.it
Wed Jun 28 18:37:12 EDT 2000


I know I'm late as you have already find the answere, but here you can find
also how to extraxt file from archives:

import zipfile
import glob, os

#Create a new zip file named new.zip and add to it
# all files in 'E:\' whose extension is '.bmp' without saving complete path
zf = zipfile.ZipFile (".\\new.zip", "w", 8)
for file in glob.glob ("e:\\*.bmp"):
     # First parameter is the file to add to the archive, second is the name
of
     # the file in archive (here is the first name without path).
     zf.write (file, os.path.split (file)[1])
zf.close ()

#Reopen the zipped file and extract the content in c:\
zf =zipfile.ZipFile (".\\new.zip", "r")
for file in zf.listdir ():
     newFile = open ("c:\\" + file, "wb")
     newFile.write (zf.read (file))
     newFile.close()
zf.close()

This work with python 1.5.2. I know in 1.6 ther's a zipfile.py module built
in,
but I don't know if his interface is the same as the old module.

-Mario

"ColBoy" <colinmeeks at home.com> ha scritto nel messaggio
news:3%d65.680$ZI2.26352 at news1.rdc1.on.wave.home.com...
> Could anybody give me a dummies guide to using the zipfile.py module.
Just
> a couple of examples of viewing a zips contents and extracting all of a
zips
> contents.  I know this is probably dead simple, but I'm still working my
way
> around Pythons syntax for stuff.  Many thanks in advance.
>
> Regards
>
> Colin
>
>





More information about the Python-list mailing list