使用子进程 python 库使用 7zip 解压缩文件 [英] Use subprocess python library to unzip a file using 7zip

查看:38
本文介绍了使用子进程 python 库使用 7zip 解压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 7zip 可执行文件用 Python 解压缩文件.在 Perl 中,这非常简单:

I would like to unzip a file with Python using 7zip executable. In Perl this is pretty straightforward:

$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'])

当我这样做时,我收到此错误:

When I do so I get this error:

FileNotFoundError: [WinError 2] The system cannot find the file specified

感谢您的帮助!

推荐答案

错误是您正在传递 strings 'zip_exe_path''logfile_path' 而不是这些变量的值.

The error is that you are passing the strings 'zip_exe_path' and 'logfile_path' instead of the values of those variables.

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 不会添加任何值并且 会产生一些开销(和风险!)

You can of course pass the command as a single string with shell=True but the shell does not add any value and incurs some overhead (and risk!)

这篇关于使用子进程 python 库使用 7zip 解压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆