By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

unzip zip files

P: n/a
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()

May 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

May 12 '06 #2

P: n/a
Marcelo Ramos wrote:
DataSmash escribió:
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.


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


or change the working dir before calling unzip.

--
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
May 12 '06 #3

P: n/a

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.

May 13 '06 #4

P: n/a
Thanks!
I ended up using the "-d" parameter.
I did try the zipfile module but I couldn't figure it out, nor could I
find any examples using it.
I also didn't have any luck changing the working dircectory and making
it work.

import subprocess, os

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

# Make system call "unzip"
print "\n unzip -d", zipBase, zip
subprocess.Popen(["unzip", "-d", zipBase, zip]).wait()

May 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.