Python subprocess argument with equal sign and space

后端 未结 2 1209
有刺的猬
有刺的猬 2021-01-05 09:08

I am trying to run a command with subprocess.check_output without using shell=True keyword argument. My command is this:

command --         


        
2条回答
  •  花落未央
    2021-01-05 09:18

    Here's what you need to know:

    Spaces are used for separating arguments on the shell command line. However, if you are not using shell, you don't need to escape spaces. Spaces can be escaped at least two ways ( that I know of ): With quotes ( either single or double ) and the backslash .

    When you pass an array to subprocess.check_output() you are already dividing the command into parameters for the subprocess. Thus, you don't need the quotes around "something with spaces". That is, you don't need to escape the spaces. Rather, the quotes are being taken quite literally as quotes as you have shown with your result snippet:

    command "--parameter=\"something with spaces\""
    

    By now I hope you have guessed what the right answer is. Spoiler ahead:

    subprocess.check_output(['command', '--parameter=something with spaces'])
    

提交回复
热议问题