Python 7z password with special characters

I am trying to unzip a file using 7z.exe, and the password contains special characters on it

EX. &)kra932(lk0¤23

By executing the following command:

subprocess.call(['7z.exe', 'x', '-y', '-ps^&)kratsaslkd932(lkasdf930¤23', 'file.zip'])

7z.exe It starts normally, but it says that the password is incorrect.

This is the file that I created, and it turns me on.

If I run the command on a windows command prompt, it works fine

7z.exe x -y -ps^&)kratsaslkd932(lkasdf930¤23 file.zip

How can I make python escape the character &?


@Wim there is a problem and password because when I execute

7z.exe x -y -ps^&)kratsaslkd932(lkasdf930¤23 file.zip 

he says an invalid ')kratsaslkd932(lkasdf930¤23' im command using python 2.76, possibly upgrading to 3.x due to company tools that only work on 2.76

+5
source share
3 answers

shlex ( Windows) , ASCII.

import shlex
import subprocess

cmd = r'7z.exe x -y -p^&moreASCIIpasswordchars file.zip'
subprocess.call(shlex.split(cmd))

, ASCII...

, Python < 3 , ASCII. C, 2.7 3.3. "" char, char.

0

, cmd , , , .

, 7z.exe x -y -ps^&)kratsaslkd932(lkasdf930¤23 file.zip ).

7z.exe x -y -p"s^&)kratsaslkd932(lkasdf930¤23" file.zip .

0

. ( ) . - 7zip .

, "source.txt" "dest.7z":

CMD = ['c:\\Program Files\\7-Zip\\7z.exe', 'a', '-t7z', '-p', 'c:\\source.txt', 'd:\\dest.7z']
PASSWORD = "Nj@8G86Tuj#a"

. , 7-zip , . , . ! , Windows "cp1250". - , ("" Python 3). , ascii :

input = (PASSWORD + "\r\n").encode("ascii")

, . , . (, , , .)

( , , Windows. , , UTF-8, , .)

:

import subprocess
import typing

def execute(cmd : typing.List[str], input: typing.Optional[bytes] = None, verbose=False, debug=False, normal_priority=False):
    if verbose:
        print(cmd)
    creationflags = subprocess.CREATE_NO_WINDOW
    if normal_priority:
        creationflags |= subprocess.NORMAL_PRIORITY_CLASS
    else:
        creationflags |= subprocess.BELOW_NORMAL_PRIORITY_CLASS

    if debug:
        process = subprocess.Popen(cmd, shell=False, stdout=sys.stdout, stderr=sys.stderr, stdin=subprocess.PIPE,
                                   creationflags=creationflags)
    else:
        process = subprocess.Popen(cmd, shell=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
                                   stdin=subprocess.PIPE, creationflags=creationflags)
    if input:
        process.stdin.write(input)
        process.stdin.flush()
    returncode = process.wait()
    if returncode:
        raise OSError(returncode)


CMD = ['c:\\Program Files\\7-Zip\\7z.exe', 'a', '-t7z', '-p', 'c:\\source.txt', 'd:\\dest.7z']
PASSWORD = "Nj@8G86Tuj#a"
input = (PASSWORD + "\r\n").encode("ascii")
execute(CMD, input)

, ( ), , .

7-zip DLL API. ( , 8- .)

Note: this example is for Python 3, but the same thing can be done with Python 2.

0
source

All Articles