unzip zip files

D

DataSmash

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

Marcelo Ramos

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.
 
J

Jay Parlar

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.
 
D

DataSmash

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top