7zip Commands from Python

后端 未结 3 1903
礼貌的吻别
礼貌的吻别 2021-02-07 13:52

There is a post on this topic already, but it does not have an explicit answer to the fundamental question which I am re-asking here:

How do you make 7zip commands from

相关标签:
3条回答
  • 2021-02-07 14:13
    import subprocess
    cmd = ['7z', 'a', 'Test.7z', 'Test', '-mx9']
    sp = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
    
    0 讨论(0)
  • 2021-02-07 14:22

    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).

    0 讨论(0)
  • 2021-02-07 14:22

    The following one works for me, python 3.5.2, windows8.1, 7z path added to system

        rc = subprocess.call(['7z', 'a', output_filename + '.zip', '-mx9', '-pSecret^)'] + [src_folder + '/'])
    

    With two parameters, -mx9 means max compression, -pSecret^) means password is "Secret^)" , "^" is escape for ")"for windows system, but when you unzip, it will need type in the "^".

    Without "^", windows system will not apply the password when 7z.exe creating the zip file.

    Also, if you want to use "-mhe" switch, you need file format in 7z instead of zip.

    0 讨论(0)
提交回复
热议问题