我想使用7zip可执行文件用Python解压缩文件。在Perl中,这非常简单:
$zip_exe_path = "C:\\Dropbox\\7-zip\\7z.exe";
$logfile_path = "C:\\Temp\\zipped_file.7z";
system ("$zip_exe_path x $log_file_path -y");
我试过了:
import subprocess
zip_exe_path = "C:\\Dropbox\\7-zip\\7z.exe"
logfile_path = "C:\\Temp\\zipped_file.7z"
subprocess.call(['zip_exe_path','x','logfile_path','-y'])
当我这样做时,我收到此错误:
FileNotFoundError: [WinError 2] The system cannot find the file specified
感谢您的帮助!
答案 0 :(得分:3)
为什么不使用python zip :
import zipfile
with zipfile.ZipFile(logfile_path, 'r') as z:
z.extractall()
或使用子流程:
subprocess.call(['zip_exe_path','x','logfile_path','-y'], shell=True)
答案 1 :(得分:3)
这对我来说就像是一件魅力:)
首先,安装py7zr库:
pip install py7zr
用于提取.7z中的所有文件:
from py7zr import py7zr
with py7zr.SevenZipFile('7z file_location', mode='r') as z:
z.extractall()
用于提取单个文件:
from py7zr import py7zr
with py7zr.SevenZipFile('7z file_location', mode='r') as z:
z.extract(targets=['rootdir/filename'])
答案 2 :(得分:1)
想出来:
import subprocess
subprocess.Popen(zip_exe+' x '+file+' -o'+output_loc,stdout=subprocess.PIPE)
答案 3 :(得分:0)
错误是您传递的是字符串 'zip_exe_path'
和'logfile_path'
,而不是这些变量的值。
import subprocess
zip_exe_path = "C:\\Dropbox\\7-zip\\7z.exe"
logfile_path = "C:\\Temp\\zipped_file.7z"
subprocess.call([zip_exe_path, 'x', logfile_path, '-y'])
您当然可以使用shell=True
作为单个字符串传递命令,但是shell不会添加任何值,并且incurs some overhead (and risk!)