Extracting multiple zip files in a directory
John Machin
sjmachin at lexicon.net
Wed May 18 22:36:09 EDT 2005
On 18 May 2005 17:30:58 -0700, "Lorn" <efoda5446 at yahoo.com> wrote:
>I've been working on this code somewhat succesfully, however I'm unable
>to get it to iterate through all the zip files in the directory. As of
>now it only extracts the first one it finds. If anyone could lend some
>tips on how my iteration scheme should look, it would be hugely
>appreciated. Here is what I have so far:
>
>import zipfile, glob, os
>
>from os.path import isfile
>fname = filter(isfile, glob.glob('*.zip'))
>for fname in fname:
Here's your main problem. See replacement below.
> zf =zipfile.ZipFile (fname, 'r')
> for file in zf.namelist():
> newFile = open ( file, "wb")
> newFile.write (zf.read (file))
> newFile.close()
> zf.close()
zipnames = filter(isfile, glob.glob('*.zip'))
for zipname in zipnames:
zf =zipfile.ZipFile (zipname, 'r')
for zfilename in zf.namelist(): # don't shadow the "file" builtin
newFile = open ( zfilename, "wb")
newFile.write (zf.read (zfilename))
newFile.close()
zf.close()
Instead of filter, consider:
zipnames = [x for x in glob.glob('*.zip') if isfile(x)]
Cheers,
John
More information about the Python-list
mailing list