Hi All--

I am working with zip archives, using the zipfile module to extract all the files with certain file extensions from each zip. In these zip archives the files I'm after all have their own path information, but I would like to extract the files I want into one simple directory without recreating the path information in the original archive. Here's the relevant snippet of code that does the extracting:

currentzip = zipfile.ZipFile(os.path.join(zdir, zfile), mode='r')
    innernames = currentzip.namelist()
    for name in innernames:
        if name[-3:] in extensionsiwant:
            currentzip.extract(name, destdir)
    currentzip.close()

zdir is the directory with the zip archives, and zfile is the archive it's working on at the moment. extensionsiwant is a list of the three-character file extensions I am interested in extracting from the archive and destdir is the directory I want to dump all the unzipped files into.

It works great, except it creates all these sub directories inside the destination directory when all I want is files.

I suppose it wouldn't be hard to write more code to move all the files and delete all the unwanted directories, but it seems like the zipfile module ought to be able to just extract the files how I want them the first time around.

Any ideas?

Thanks!!!

Recommended Answers

All 3 Replies

Effbot's example for reading a file into memory, which can then be written anywhere you like as long as the files are not huge.

import zipfile

file = zipfile.ZipFile("samples/sample.zip", "r")

for name in file.namelist():
    data = file.read(name)
    print name, len(data), repr(data[:10])

This is another version.

You take read the zipfile and use the file type you want to create a new zip file. Simple.

import zipfile

if __name__ =="__main__":
    ext=["py","txt","doc"]
    nfile=[]  #to take new file list
    ffile=zipfile.ZipFile("/home/richie/r.zip","w") # New zip
    zfile= zipfile.ZipFile("/home/richie/GUI2Exe.zip","r")# Main zip
    for xfile in zfile.namelist():
        if xfile.split(".")[-1] in ext: #Check extention
            nfile.append(xfile)         #append
            ffile.write("/home/richie/"+xfile)  # write new one
    print "New zip file data: ", nfile      
    ff.close()
    zfile.close()

Many thanks to both of you!

Using .read to get the file's bytes, then write out the file how I want will do the trick. It will also let me handle the problem of duplicate file names the way I want to.

Richie, your test for the extension is much better than mine, since it allows for extensions of any length, and since mine could be easily fooled by a file named "thisfileisnotadoc" for example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.