How to extract a 7z zip file in Python 2.7.3

How to extract a 7z zip file in python. Please let anyone know if there is a library for this.

I have libarchive library installed in python 2.7.3 version. But I cannot use this library.

+3


source to share


4 answers


You can use the library PyLZMA

and py7zlib

to retrieve the file 7z

, or try to execute a shell script to extract the zip file using python module subprocess

.



+3


source


I am using command as 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)

      

+2


source


According to the Python doc (about subprocess) , you can use the recommended execute function (like in this example).

from subprocess import run
run('C:\\Program Files\\7-Zip\\7zG.exe x'+ archive_name + ' -o' + folder_name_to_extract)`

      

PS0: . Be careful not to forget to avoid characters in full path; it can help a lot under Windows. Otherwise, the OS couldn't find 7zip (or another program).

PS1: Apparently, the comments are difficult to write ... The display was not the same (like all the text in just one line), and through touch input, the message would be published (unfinished). The system from stackoverflow.com is wrong because I wanted to just add a few lines and not post it. And also because no, at the moment I have not finished writing (this post).

0


source


This worked for me on Windows. The line you want to shoot looks 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/"

      

Note the call to exe, and the parameters are not quoted, 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)

      

0


source







All Articles