unzip zip files

DataSmash rdh at new.rr.com
Fri May 12 17:45:49 EDT 2006


I need to unzip all zip file(s) in the current directory
into their own subdirectories.  The zip file name(s) always
start with the string "usa" and end with ".zip".
The code below will make the subdirectory, move the zip
file into the subdirectory, but unzips the contents into the
root (current) directory.  I want the contents of the zip file
unloaded into the newly created subdirectory where the zip file is.

Any ideas?
Thanks.
R.D.

import subprocess

# Get all the zip files in the current directory.
    for zip in os.listdir(''):
        if zip.endswith(".zip"):

            # Remove the first 3 and the last 4 characters
            # e.g. usa12345.zip becomes 12345
            zipBase = zip[3:-4]

            # Make directory for unzipping
            os.mkdir(zipBase)

            # Move the zip file to the subdirectory
            shutil.move(zip, zipBase)

            # Make system call "unzip"
            subprocess.Popen(["unzip", zipBase + "\\" + zip]).wait()




More information about the Python-list mailing list