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,
JavaScript
x
29
29
1
import subprocess
2
import os
3
4
currentFile = __file__
5
realPath = os.path.realpath(currentFile)
6
dirPath = os.path.dirname(realPath)
7
dirName = os.path.basename(dirPath)
8
9
sevenz = dirPath + '/binaries/7z.exe'
10
files = []
11
12
for filename in os.listdir():
13
ext = os.path.splitext(filename)[-1].lower()
14
if ext == '.rar':
15
files.append(filename)
16
if ext == '.zip':
17
files.append(filename)
18
if ext == '.7z':
19
files.append(filename)
20
if ext == '7z.001':
21
files.append(filename)
22
23
with open('passwords.txt', 'r') as f:
24
passwords = [line.rstrip() for line in f]
25
26
for password in passwords:
27
for file in files:
28
subprocess.run([sevenz, 'x', '-y', '-p', password, file])
29
And below is the content of passwords.txt file,
JavaScript
1
4
1
zip
2
7z
3
rar
4
Running above code gives this error,
JavaScript
1
7
1
7-Zip 21.07 (x64) : Copyright (c) 1999-2021 Igor Pavlov : 2021-12-26
2
3
Scanning the drive for archives:
4
5
ERROR: The system cannot find the file specified.
6
zip
7
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.
JavaScript
1
2
1
subprocess.run([sevenz, 'x', '-y', '-p'+password, file])
2