使用python测试windows批处理文件

2024-09-26 22:54:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个批处理windows批处理文件。批处理文件运行测试.exe. 我用过子流程.popen使用python运行批处理文件。批处理文件在另一个窗口中运行,批处理文件在另一个窗口中运行测试.exe. 这个测试.exe批处理文件在不同的窗口中运行时会显示“按任意键继续”。我可以用python传递击键吗

import subprocess
subprocess.Popen(r'start cmd /c C:\test.bat', shell=True)
p.wait()
print 'done'

Tags: 文件testimportcmdwindows流程exestart
1条回答
网友
1楼 · 发布于 2024-09-26 22:54:09

Python不能直接满足您的需求,但是Windows包含了您所需要的一切,并且可以通过标准库的ctypes模块从Python获得WinAPI函数。你知道吗

您需要:

  • 找到承载子命令的窗口的句柄
  • 向该窗口发送一个字符(例如enter键)

根据批处理的启动方式,并假设安装了标准的Windows,应该可以做到以下几点:

# find the window handle from the windows title (should be FindWindowA for bytes in Python 2)
hwnd = ctypes.windll.User32.FindWindowW(None, r'C:\Windows\system32\cmd.exe')
if hwnd == 0:
    raise ValueError("Window title not found")

# send a Return to that window (0x102 is WM_CHAR and 0x0D is VK_ENTER)
ctypes.windll.User32.PostMessageW(hwnd, 0x102, 0x0d, 1)

相关问题 更多 >

    热门问题