2014-03-07 17 views
2

如果我从cmd.exe运行下面的命令,我得到的,看起来像这样的错误消息:为什么`subprocess.Popen`返回一个不同的输出到shell? - 视窗

C:\Users\user>ctaags --help 
'ctaags' is not recognized as an internal or external command, 
operable program or batch file. 

现在,如果我使用Python的subprocess模块运行相同的命令,我得到一个不同的警告:

>>> subprocess.Popen(['ctaags', '--help']) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "c:\Python27\lib\subprocess.py", line 711, in __init__ 
    errread, errwrite) 
    File "c:\Python27\lib\subprocess.py", line 948, in _execute_child 
    startupinfo) 
WindowsError: [Error 2] The system cannot find the file specified 

基于其他命令,我期望从Popen输出完全匹配什么样的壳给我回来,即:

WindowsError: [Error 2] 'ctaags' is not recognized as an internal or 
external command, operable program or batch file. 

情况并非如此。为什么?

+1

是否有理由在每个ctaa ... aags中有不同数量的a? – icedtrees

+0

不是 - 现在修好了。 – stephenfin

回答

2

运行进程的系统调用返回一个错误2无论它是由外壳或Python的调用。

在外壳的情况下,它是外壳,然后生成“未识别”的消息。

使用subprocess.Popen()shell=True(你应该没有必要不使用),无壳存在,所以你得到的原始错误代码回自己。

1

POPEN不使用shell,除非你设置壳不执行命令= TRUE:

subprocess.Popen(['ctaaags', '--help'], shell=True) 
+0

...但鼓励人们使用'shell = True'是不好的做法;一般而言,它为错误,安全影响和其他方面带来了空间,因为意图成为程序参数的东西可能被shell(错误)解释。 –

+0

不要使用'shell = True'的列表参数。它在Unix上完全不同。除非你想运行一个内置命令,否则你不需要'shell = True'。 – jfs

相关问题