Subprocess.CREATE_NEW_CONSOLE(Subprocess.CREATE_NE

2019-10-22 15:27发布

我有这样的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

Answer 1:

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

会做你想要什么,我推测是打开一个shell并执行airmon-ng check kill通过Python的。 我通过看子文档和指向唯一CREATE_NEW_CONSOLE指出creationflags仅可在Windows这可以解释为什么它不能在Linux Mint的工作。 对于CREATE_NEW_CONSOLE该文档还指出,

新的进程有一个新的控制台,而不是继承其父的控制台(默认值)。 当创建POPEN该标志总是被设置shell=True

因此,只要使用shell=True就是做你想要的东西的最佳方式。



文章来源: Subprocess.CREATE_NEW_CONSOLE