我的印象是exit(0)
向Python解释器发出信号,将0返回给系统。例如,
from subprocess import check_call
check_call('python3 -c "exit(0)"', shell=True) # returns 0
但是
check_call(['/usr/bin/python3', '-c "exit(0)"'])
返回1:
>>> check_call(['/usr/bin/python3', '-c "exit(0)"'])
File "<string>", line 1
"exit(0)"
^
IndentationError: unexpected indent
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/subprocess.py", line 581, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/usr/bin/python3', '-c "exit(0)"']' returned non-zero exit status 1
我无法告诉任何空间在哪里潜入。发生了什么事?
答案 0 :(得分:4)
看来如果-c
标志后面没有另一个参数,则当前参数的其余部分被解释为Python代码:
>> python3 -c 'print("yes")'
yes
>> python3 '-cprint("yes")'
yes
>> python3 '-c print("yes")'
File "<string>", line 1
print("yes")
^
IndentationError: unexpected indent
因此,以下两个方法都应该有效,尽管第一个变体感觉最惯用/安全:
check_call(['/usr/bin/python3', '-c', 'exit(0)'])
check_call(['/usr/bin/python3', '-cexit(0)'])