Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:在linux上,subprocess.Popen()与shell=True一起工作_Python - Fatal编程技术网

Python:在linux上,subprocess.Popen()与shell=True一起工作

Python:在linux上,subprocess.Popen()与shell=True一起工作,python,Python,如果我在Windows上执行以下python代码: import subprocess subprocess.Popen( [ 'python', 'foo' ], shell = True ).communicate() 我得到了写入标准输出的错误,正如预期的那样: python: can't open file 'foo': [Errno 2] No such file or directory 但是如果我在linux上执行相同的代码(ubuntu,OSX-any),我会启动交互式Pyt

如果我在Windows上执行以下python代码:

import subprocess
subprocess.Popen( [ 'python', 'foo' ], shell = True ).communicate()
我得到了写入标准输出的错误,正如预期的那样:

python: can't open file 'foo': [Errno 2] No such file or directory
但是如果我在linux上执行相同的代码(ubuntu,OSX-any),我会启动交互式PythonRepl而不是本文!像这样:

user@debian:~/Documents$ python test.py
Python 2.7.3 (default, Jab 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information
>>>

为什么会有如此奇怪的行为?在所有平台上执行带参数('foo')的python解释器必须将其置于求值模式,而不是REPL模式。

这在文档中有详细说明:

shell参数(默认为False)指定是否将shell用作要执行的程序如果shell为True,建议将参数作为字符串而不是序列传递。

在shell=True的Unix上,shell默认为/bin/sh。如果args是字符串,则该字符串指定要通过shell执行的命令。这意味着字符串的格式必须与在shell提示下键入时的格式完全相同。例如,这包括引用或反斜杠转义文件名,其中包含空格。如果args是一个序列,则第一项指定命令字符串,任何附加项都将被视为shell本身的附加参数。

(强调矿山)