unzip zip files

Marcelo Ramos mramos at montevideo.com.uy
Fri May 12 18:48:00 EDT 2006


DataSmash escribió:
> 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()
>
>   

See "-d" zip's parameter in man zip.

Regards.

-- 
Marcelo Ramos
Fedora Core 5 | 2.6.16
Socio UYLUG Nro 125




More information about the Python-list mailing list