如何在 Python 2.7.3 版本中提取 7z zip 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26603204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract 7z zip file in Python 2.7.3 version
提问by Ibney Hasan
How to extract 7z zip file in python .Please some one let me know is there any library for that.
如何在 python 中提取 7z zip 文件。请告诉我是否有任何库。
I have install libarchive library in python 2.7.3 version . But i am not able to use that library.
我已经在 python 2.7.3 版本中安装了 libarchive 库。但我无法使用该库。
回答by hamidfzm
You can use PyLZMAand py7zliblibraries to extract 7zfile or try executing shell scripts to extract zip file using python subprocessmodule.
您可以使用PyLZMA和py7zlib库来提取7z文件或尝试使用 pythonsubprocess模块执行 shell 脚本来提取 zip 文件。
回答by Vasily Ryabov
I use command like C:\Program Files\7-Zip\7z.exe x <filename>in my C++ project. You can run it in Python like so:
我C:\Program Files\7-Zip\7z.exe x <filename>在我的 C++ 项目中使用命令。你可以像这样在 Python 中运行它:
import subprocess
subprocess.call(r'"C:\Program Files-Zipz.exe" x ' + archive_name + ' -o' + folder_name_to_extract)
or 32-bit version:
或 32 位版本:
subprocess.call(r'"C:\Program Files (x86)-Zipz.exe" x ' + archive_name + ' -o' + folder_name_to_extract)
回答by Wagner_SOFC
According to the Python doc (about the subprocess), you might rather to use the recommanded function run(such as in this exemple).
根据Python doc (about the subprocess),您可能宁愿使用推荐的函数run(例如在此示例中)。
from subprocess import run
run('C:\Program Files\7-Zip\7zG.exe x'+ archive_name + ' -o' + folder_name_to_extract)`
PS0 :One adivce, don't forget to escape the chars in the full path; it's could help a lot specially under Windows. Otherwise the OS couldn't find 7zip (or another program).
PS0 :一个建议,不要忘记对完整路径中的字符进行转义;它在 Windows 下特别有用。否则操作系统找不到 7zip(或其他程序)。
PS1 :Apparently, the comments are difficult to wrote... The display wasn't the same (like group all the text just in only one line) and via the touch enter, the post will be publish (unfinished). The system from stackoverflow.com is wrong because I wanted to just add a few lines and not publish it. And also because no, on the moment, I wasn't finish to write (this post).
PS1:显然,评论很难写......显示不一样(就像将所有文本分组在一行中)并且通过触摸输入,帖子将被发布(未完成)。stackoverflow.com 的系统是错误的,因为我只想添加几行而不是发布它。也因为不,此刻,我还没有写完(这篇文章)。
回答by Treeswing
This worked for me in Windows. The string you want to shoot for is something like this:
这在 Windows 中对我有用。您要拍摄的字符串是这样的:
C:/Egain_ETL/7-Zip/7z.exe e "C:/Egain_ETL/EG_DATA_EXTRACT_2017-11-25 09-45-10-627.7z" -p"Dev@123" -o"C:/Egain_ETL/"
Notice the call to the exe and options are unquoted, everything else is double quoted.
请注意对 exe 和选项的调用未加引号,其他所有内容都用双引号引起来。
Sample Code:
示例代码:
import subprocess
Z_Location = 'C:/Egain_ETL/7-Zip/7z.exe'
Extract_File = 'C:/Egain_ETL/EG_DATA_EXTRACT_2017-11-25 09-45-10-627.7z'
Extract_PW = 'Dev@123'
Extact_Folder = 'C:/Egain_ETL/'
Extract_Target = Z_Location + ' e ' + '"' + Extract_File + '"' + ' -p' + '"' + Extract_PW + '"' + ' -o' + '"' + Extact_Folder + '"'
subprocess.run(Extract_Target)
回答by Ashis Kumar Panda
!apt-get install p7zip-full
!p7zip -d file_name.tar.7z
!apt-get install p7zip-full
!p7zip -d file_name.tar.7z
Try the above steps
尝试以上步骤

