问题
How to extract 7z zip file in python .Please some one let me know is there any library for that.
I have install libarchive library in python 2.7.3 version . But i am not able to use that library.
回答1:
You can use PyLZMA
and py7zlib
libraries to extract 7z
file or try executing shell scripts to extract zip file using python subprocess
module.
回答2:
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:
import subprocess
subprocess.call(r'"C:\Program Files\7-Zip\7z.exe" x ' + archive_name + ' -o' + folder_name_to_extract)
or 32-bit version:
subprocess.call(r'"C:\Program Files (x86)\7-Zip\7z.exe" x ' + archive_name + ' -o' + folder_name_to_extract)
回答3:
According to the Python doc (about the subprocess), you might rather to use the recommanded function run (such as in this exemple).
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).
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).
回答4:
This worked for me in Windows. The string you want to shoot for is something like this:
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.
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)
回答5:
!apt-get install p7zip-full
!p7zip -d file_name.tar.7z
Try the above steps
来源:https://stackoverflow.com/questions/26603204/how-to-extract-7z-zip-file-in-python-2-7-3-version