Subprocess.CREATE_NEW_CONSOLE

Subprocess.CREATE_NEW_CONSOLE

我有这个 Python 代码。

import subprocess

subprocess.Popen("airmon-ng check kill", creationflags = subprocess.CREATE_NEW_CONSOLE)

Python Linux Mint 上的 2.7.6 给我以下错误:

    subprocess.Popen("airmon-ng check kill", creationflags = subprocess.CREATE_NEW_CONSOLE)
    AttributeError: 'module' object has no attribute 'CREATE_NEW_CONSOLE'

Windows 8.1 上的相同给了我这个:

Traceback (most recent call last):
File "C:\Users\Ben\Dropbox\Coding\jam.py", line 10, in <module>
subprocess.Popen("airmon-ng check kill", creationflags = subprocess.CREATE_NEW_CONSOLE)
File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
subprocess.Popen("airmon-ng check kill", shell=True)

会做你想做的事,我想是打开一个 shell 并执行 airmon-ng check kill 到 Python。 我查看了 subprocess docs,唯一指向 CREATE_NEW_CONSOLE 的内容表明 creationflags 仅在 Windows 中可用,这可以解释为什么它在 Linux薄荷糖。 The docs for CREATE_NEW_CONSOLE 还指出

The new process has a new console, instead of inheriting its parent’s console (the default). This flag is always set when Popen is created with shell=True.

所以只使用 shell=True 是做你想做的最好的方法。