解压多个受密码保护的文件
Unzip multiple password protected files
我是 python 的新手。我正在尝试编写一个简单的 python 脚本来将多个受密码保护的文件解压缩到一个目录中。密码存储在文本文件中。
这是我想出的,
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])
下面是passwords.txt文件的内容,
zip
7z
rar
运行 上面的代码给出了这个错误,
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
我不确定我做错了什么。如有任何帮助,我们将不胜感激!
密码应连接到 -p
,而不是单独的参数。
subprocess.run([sevenz, 'x', '-y', '-p'+password, file])
我是 python 的新手。我正在尝试编写一个简单的 python 脚本来将多个受密码保护的文件解压缩到一个目录中。密码存储在文本文件中。
这是我想出的,
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])
下面是passwords.txt文件的内容,
zip
7z
rar
运行 上面的代码给出了这个错误,
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
我不确定我做错了什么。如有任何帮助,我们将不胜感激!
密码应连接到 -p
,而不是单独的参数。
subprocess.run([sevenz, 'x', '-y', '-p'+password, file])