1

Here is my code I don't know how can I loop every .zip in a folder, please help me: I want all contents of 5 zip files to extracted in one folder, not including its directory name

import os
import shutil
import zipfile
my_dir = r"C:\\Users\\Guest\\Desktop\\OJT\\scanner\\samples_raw"

my_zip = r"C:\\Users\\Guest\\Desktop\\OJT\\samples\\001-100.zip"
with zipfile.ZipFile(my_zip) as zip_file:
zip_file.setpassword(b"virus")
for member in zip_file.namelist():
    filename = os.path.basename(member)
    # skip directories
    if not filename:
        continue

    # copy file (taken from zipfile's extract)
    source = zip_file.open(member)
    target = file(os.path.join(my_dir, filename), "wb")
    with source, target:
        shutil.copyfileobj(source, target)

2 Answers2

0

repeated question, please refer below link.

How to extract zip file recursively in Pythonn

jits_on_moon
  • 827
  • 6
  • 10
0

What you are looking for is glob. Which can be used like this:

#<snip>
import glob

#assuming all your zip files are in the directory below.
for my_zip in glob.glob(r"C:\\Users\\Guest\\Desktop\\OJT\\samples\\*.zip"):
    with zipfile.ZipFile(my_zip) as zip_file:
        zip_file.setpassword(b"virus")
        for member in zip_file.namelist():
            #<snip> rest of your code here.
arunkumar
  • 32,803
  • 4
  • 32
  • 47
  • wait man it extracted the 6 folders into 6 files not the contents of the zip files –  Sep 25 '18 at 04:02
  • this gaved me 6 extracted files i don't know why, not the contents of the 6 folders –  Sep 25 '18 at 04:04
  • if you have multiple folders you could try something like this `glob.glob(r"C:\\Users\\Guest\\Desktop\\OJT\\scanner\\samples_raw\\**\\*.zip"`. This will scan directories within samples_raw zip files. – arunkumar Sep 25 '18 at 04:11
  • no what i mean is it extracted the 6 zip files into .file the contents of the zip files were not extracted –  Sep 25 '18 at 04:28
  • have a look at this question - https://stackoverflow.com/questions/3451111/unzipping-files-in-python – arunkumar Sep 25 '18 at 05:12