1

I use subprocess's check_output() function two ways,find the result are different, I don't known why.

  1. First way:

    from subprocess import check_output as qc
    output = qc(['exit', '1'], shell=True)
    
  2. Second way:

    from subprocess import check_output as qc
    output = qc(['exit 1'], shell=True)
    

Error:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/home/work/cloud/python2.7_64/lib/python2.7/subprocess.py", line 544, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['exit 1']' returned non-zero exit status 1

Second way is right, but first way why is not right?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
sungs
  • 33
  • 3

1 Answers1

1

Quoting subprocess docs:

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

What you actually do in each case is:

  1. You pass a sequence of arguments: ['exit', '1']. Sequence is equivalent of shell command exit 1. Arguments are separated by spaces and there are no quotations to alter separation process.

  2. You pass a sequence of arguments: ['exit 1'], which has length 1. This is equivalent to shell command "exit 1". Your first (and only) argument has space in it, which is analogous with enclosing it in quoting marks.

As you can verify, exit codes of both commands are different, therefore your Python script output is different.

Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93