7zip Commands from Python

You can wrap it as a function using the following:

import subprocess

def sevenzip(filename, zipname, password):
    print("Password is: {}".format(password))
    system = subprocess.Popen(["7z", "a", zipname, filename, "-p{}".format(password)])
    return(system.communicate())

This definitely works as I've tried and tested it. If you want to tweak it i.e. to Extract files then you can use the following:

def extractfiles(zipname):
    system = subprocess.Popen(["7z", "e", zipname])
    return(system.communicate())

Give this a try and lemme know how you get on.

Bear in mind this is for Linux. In Windows, swap "7z" with "C:\Program Files\7-Zip\7z.exe" (i think that's the right location).


import subprocess
cmd = ['7z', 'a', 'Test.7z', 'Test', '-mx9']
sp = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)

Tags:

Python

7Zip