user124123
user124123

Reputation: 1683

TypeError using ZipFile.extractall

I am trying to use zipfile to extract all zip files from a folder into that folder, but am getting a TypeError:

TypeError: extractall() missing 1 required positional argument: 'self'

My script looks like

import os
from zipfile import *


for file in os.listdir():
  if file.endswith(".zip"):
    ZipFile.extractall(path= "M:\path\...\path", members=file,pwd="password!")

Does anyone know why this would be the case?

Thanks

Upvotes: 1

Views: 4047

Answers (2)

Damián Montenegro
Damián Montenegro

Reputation: 839

Your code should look like this:

for f in os.listdir('.'):
    if f.endswith(".zip"):
        z = zipfile.ZipFile(f, 'r')
        z.extractall(path=os.path.dirname(f))
        z.close()

Upvotes: 0

Sait
Sait

Reputation: 19825

You are calling the ZipFile.extractall() function wrong.

You can extract one zip file using:

import zipfile

zf = zipfile.ZipFile('myzip.zip', mode='r')
zf.extractall(pwd='password'.encode('ascii'))

zf.close()

To extract all files with the ending .zip, you can do:

import zipfile
import glob

files = glob.glob('*.zip')
for f in files:
    zf = zipfile.ZipFile(f, mode='r')
    zf.extractall(pwd='password'.encode('ascii'))
    zf.close()

Upvotes: 1

Related Questions