admin管理员组文章数量:1577823
python 新建一个cmd窗口运行命令,并关闭窗口
- 2,cmd 运行python 脚本,并关闭创建的 cmd窗口
import subprocess,time,psutil
proc = subprocess.Popen("C:\\downloads\\b.bat",creationflags=subprocess.CREATE_NEW_CONSOLE)
time.sleep(10)
pobj = psutil.Process(proc.pid)
# list children & kill them
for c in pobj.children(recursive=True):
c.kill()
pobj.kill()
b.bat 内容
ping -t baidu
10秒后ping关闭窗口
2,cmd 运行python 脚本,并关闭创建的 cmd窗口
test03.py 脚本
import subprocess,time,psutil
command = "C:\\downloads\\b.bat"
proc = subprocess.Popen(command,creationflags=subprocess.CREATE_NEW_CONSOLE)
time.sleep(5)
pobj = psutil.Process(proc.pid)
# list children & kill them
for c in pobj.children(recursive=True):
c.kill()
pobj.kill()
b.bat 脚本
python "C:\\win10\\test01.py"
test01.py
import requests
from time import sleep
i = 0
while (i < 10):
print (i)
sleep(1)
i = i + 1
url = requests.get("http://www.baidu")
status = url.status_code
print (status)
参考:
- Can I close the CMD window that opened with subprocess.Popen in Python?
- Using subprocess to run Python script on Windows
- How to Create a Batch File to Run Python Script
版权声明:本文标题:windows 10 python subprocess.Popen cmd 命令 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1725556840a1029183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论