unzip zip files

Jay Parlar jparlar at cogeco.ca
Sat May 13 22:40:53 CEST 2006


On May 12, 2006, at 5:45 PM, DataSmash wrote:

> 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()
>


Ouch... Is there any reason that you're using subprocess+'unzip', as 
opposed to using the 'zipfile' module from the standard library?

Jay P.




More information about the Python-list mailing list