Subprocess.CREATE_NEW_CONSOLE

问题描述 投票:1回答:1

我有这个Python代码。

import subprocess

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

Linux Mint上的Python 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
python subprocess popen
1个回答
5
投票
subprocess.Popen("airmon-ng check kill", shell=True)

将做您想做的,我想是打开一个外壳并通过Python执行airmon-ng check kill。我浏览了subprocess docs,唯一指向CREATE_NEW_CONSOLE的地方指出creationflags仅在Windows中可用,这说明了为什么它在Linux Mint中不起作用。The docs for CREATE_NEW_CONSOLE还指出

新进程具有一个新的控制台,而不是继承其父级的控制台(默认)。当使用shell=True创建Popen时,始终设置此标志。

因此,仅使用shell=True是完成所需操作的最佳方法。

© www.soinside.com 2019 - 2024. All rights reserved.