如果我在Windows上执行以下python代码:
import subprocess
subprocess.Popen( [ 'python', 'foo' ], shell = True ).communicate()
Run Code Online (Sandbox Code Playgroud)
我按预期将错误写入stdout:
python: can't open file 'foo': [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)
但是如果我在linux上执行相同的代码(ubuntu,OSX - any),我开始使用交互式python REPL而不是这个文本!像这样:
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
>>>
Run Code Online (Sandbox Code Playgroud)
为何如此奇怪的行为?使用参数('foo')执行python解释器必须在所有平台上进入评估模式,而不是进入REPL模式.
这在文档中有详细说明:
shell参数(默认为False)指定是否将shell用作要执行的程序.如果shell为True,建议将args作为字符串而不是序列传递.
在Unix上,shell = True,shell默认为/ bin/sh.如果args是一个字符串,则该字符串指定要通过shell执行的命令.这意味着字符串的格式必须与在shell提示符下键入时完全相同.这包括,例如,引用或反斜杠转义带有空格的文件名.如果args是一个序列,则第一个项指定命令字符串,并且任何其他项将被视为shell本身的附加参数.
(强调我的)
| 归档时间: |
|
| 查看次数: |
1460 次 |
| 最近记录: |