打开空命令提示符并使用 Python 保持打开状态
Open Empty Command Prompt and Stay Open with Python
我想要 python 打开一个空的命令提示符 (cmd.exe) 并在没有任何 运行 的情况下保持打开状态。我希望在新的 window 中打开命令提示符,并且此 python 代码继续 运行。
我试过:
os.system("start C://Windows/System32/cmd.exe")
并且:
os.system("C://Windows/System32/cmd.exe")
并且:
os.system("start /wait C://Windows/System32/cmd.exe")
并且:
os.system("start /wait cmd /c")
None 上面左边的命令提示符打开。我还希望能够稍后通过使用关闭它(使用 python):
os.system("taskkill /f /im cmd.exe")
感谢您的帮助。我在任何地方都找不到这个问题的答案。 This 是最接近的,但这需要输入命令。我不想之前输入命令。
对我来说,这有效(Windows 10,Python 3.3.5 并使用 psutil 库)。
import psutil
import subprocess
import time
import os
p = subprocess.Popen("start /wait cmd.exe", shell=True)
# the pid of p is the pid of the shell, so let's get the shell's children,
# i.e. the opened cmd.exe window
p2 = psutil.Process(p.pid)
# there should be exactily one child
# but it may not have been started yet
while True:
children = p2.children()
if children:
break
print("no children yet")
time.sleep(0.01)
print(children)
time.sleep(5)
for child in children:
child.kill()
p.wait()
我找到了一些方法来做到这一点。首先,有两种打开命令提示符的方式。一种是正常的 Windows 命令 Prompt.The 另一种方法是使用 python 作为输入打开它。两者都有几种方法。
对于Python作为输入:
os.system('cmd.exe /C start "Unique_name" cmd.exe')
"unique_name" 可以用以下方式关闭:
os.system('taskkill /F /IM "cmd.exe" /FI "WINDOWTITLE eq Unique_name"')
以下也有效:
os.system('cmd /C start cmd')
os.system('cmd /C start')
os.system('start cmd')
对于 Windows 命令提示符:
os.startfile("C://Windows/System32/cmd.exe")
可以通过以下方式关闭:
os.system("taskkill /f /im cmd.exe")
我想要 python 打开一个空的命令提示符 (cmd.exe) 并在没有任何 运行 的情况下保持打开状态。我希望在新的 window 中打开命令提示符,并且此 python 代码继续 运行。
我试过:
os.system("start C://Windows/System32/cmd.exe")
并且:
os.system("C://Windows/System32/cmd.exe")
并且:
os.system("start /wait C://Windows/System32/cmd.exe")
并且:
os.system("start /wait cmd /c")
None 上面左边的命令提示符打开。我还希望能够稍后通过使用关闭它(使用 python):
os.system("taskkill /f /im cmd.exe")
感谢您的帮助。我在任何地方都找不到这个问题的答案。 This 是最接近的,但这需要输入命令。我不想之前输入命令。
对我来说,这有效(Windows 10,Python 3.3.5 并使用 psutil 库)。
import psutil
import subprocess
import time
import os
p = subprocess.Popen("start /wait cmd.exe", shell=True)
# the pid of p is the pid of the shell, so let's get the shell's children,
# i.e. the opened cmd.exe window
p2 = psutil.Process(p.pid)
# there should be exactily one child
# but it may not have been started yet
while True:
children = p2.children()
if children:
break
print("no children yet")
time.sleep(0.01)
print(children)
time.sleep(5)
for child in children:
child.kill()
p.wait()
我找到了一些方法来做到这一点。首先,有两种打开命令提示符的方式。一种是正常的 Windows 命令 Prompt.The 另一种方法是使用 python 作为输入打开它。两者都有几种方法。
对于Python作为输入:
os.system('cmd.exe /C start "Unique_name" cmd.exe')
"unique_name" 可以用以下方式关闭:
os.system('taskkill /F /IM "cmd.exe" /FI "WINDOWTITLE eq Unique_name"')
以下也有效:
os.system('cmd /C start cmd')
os.system('cmd /C start')
os.system('start cmd')
对于 Windows 命令提示符:
os.startfile("C://Windows/System32/cmd.exe")
可以通过以下方式关闭:
os.system("taskkill /f /im cmd.exe")