Skip to main content
 首页 » 编程设计

python之子进程.CREATE_NEW_CONSOLE

2025年05月04日80telwanggs

我有这段 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 

请您参考如下方法:

subprocess.Popen("airmon-ng check kill", shell=True) 

会做你想做的事,我想这就是打开一个 shell 并通过 Python 执行 airmon-ng check kill。 我浏览了 subprocess docs并且唯一指向 CREATE_NEW_CONSOLE 的内容表明 creationflags 仅在 Windows 中可用,这可以解释为什么它在 Linux Mint 中不起作用。 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 是做你想做的最好的方法。