0

I have a python script that should run 7z.exe with the command: "x" and switch " -o" using subprocess.run(). The script is as follows:

import subprocess as sb


zipperpath = "C:\\Program Files\\7-zip\\7z.exe"
dirname ="C:\\Users\\ajain\\Desktop\\TempData"
archivename="UnprocessedData_v3.7z"
outputfilename="foo"

sb.run([zipperpath,"x",os.path.join(dirname,archivename)," -o",os.path.join(dirname,outputfilename)])

Output is: enter image description here Although the return code is 0, the zip never gets unzipped.

Abhishek Jain
  • 445
  • 2
  • 6
  • 19
  • 2
    I see 2 potential issues. First, you are passing " -o" instead of "-o". The second is the docs indicate that the `-o` and the directory need to be combined into the same arg. – jordanm Dec 20 '19 at 00:59
  • I would like to think there's a native python 7zip extractor. https://stackoverflow.com/questions/44132184/extract-7z-file-using-python-3 – OneCricketeer Dec 20 '19 at 01:00
  • Can you add how is your command like you put in your console..?....work directly? – GiovaniSalazar Dec 20 '19 at 01:35
  • @GiovaniSalazar The comment by "jordanm" perfectly resolves the issue. The command whether executed directly on the console or the program was not working correctly because of the syntax issue. – Abhishek Jain Dec 20 '19 at 23:08

1 Answers1

-1

Try with this :

import subprocess

# variable cmd is is your command line , like you put in your console 
cmd='7z.exe x UnprocessedData_v3.7z'

process = subprocess.Popen(cmd,shell=True,stdin=None,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# The output from your shell command in an array
result=process.stdout.readlines()
if len(result) >= 1:
    for line in result:
        print(line)
GiovaniSalazar
  • 1,999
  • 2
  • 8
  • 15