Python & zipfiles

JohnJacob noone at here.invalid
Tue Jun 25 14:34:09 EDT 2002


"Patrick Bielen" <bielen at stafa.nl> wrote in message
news:uhgfcopr269e0 at corp.supernews.com...
> Hello all,
>
> I use a module to work with zipfiles.
> This one is only for unpacking files
> from the zip. But i only want to know
> if i have to much code to do the trick.
>
> I'm barely new to python, that's why i ask.
>
> --- begin script
>
> import os
> from zipfile import ZipFile
>
> zipdir = '/as400/Beheer/Databases/'
> os.chdir('/tmp')
> zipfiles = ZipFile(zipdir + 'dbs.zip').namelist()
> ZipFile(zipdir + 'dbs.zip','r')
> for file in zipfiles:
>     print file
>     source = ZipFile(zipdir + 'dbs.zip').read(file)
>     open(file, 'w').write(source)
> ZipFile(zipdir + 'dbs.zip').close()
>
> --- end of script ---
>
> The problem is, that i don't know if i use the
> ZipFile-command too much... because the manual
> told me i have to close it afterwards, but it's
> not clear to me if i have to expecitely open it.
>
> Any help or code cleaning would be really appre-
> ciated.
>
> Greetings
>
> Patrick

This is how I would rewrite it:

import os
from zipfile import ZipFile

zipdir = '/as400/Beheer/Databases/'
os.chdir('/tmp')

zipfile = ZipFile(os.path.join(zipdir, 'dbs.zip'))
for file in zipfiles.namelist():
    print file
    source = zipfile.read(file)
    open(file, 'w').write(source)

zipfile.close()



greg





More information about the Python-list mailing list