Skip to content
Advertisement

Unzip multiple password protected files

I’m a newbie in python. I’m trying to write a simple python script to unzip multiple password protected files in a directory. Passwords are stored in a text file.

This is what i have come up with,

import subprocess
import os

currentFile = __file__
realPath = os.path.realpath(currentFile)
dirPath = os.path.dirname(realPath)
dirName = os.path.basename(dirPath)

sevenz = dirPath + '/binaries/7z.exe'
files = []

for filename in os.listdir():
    ext = os.path.splitext(filename)[-1].lower()
    if ext == '.rar':
        files.append(filename)
    if ext == '.zip':
        files.append(filename)
    if ext == '.7z':
        files.append(filename)
    if ext == '7z.001':
        files.append(filename)           

with open('passwords.txt', 'r') as f:
  passwords = [line.rstrip() for line in f]

for password in passwords:
    for file in files:
        subprocess.run([sevenz, 'x', '-y', '-p', password, file])

And below is the content of passwords.txt file,

zip
7z
rar

Running above code gives this error,

7-Zip 21.07 (x64) : Copyright (c) 1999-2021 Igor Pavlov : 2021-12-26

Scanning the drive for archives:

ERROR: The system cannot find the file specified.
zip

I’m not sure what i’m doing wrong here. Any help would be appreciated!

Advertisement

Answer

The password should be concatenated to -p, not a separate argument.

        subprocess.run([sevenz, 'x', '-y', '-p'+password, file])
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement