如何在Windows上使用python3执行交互式.exe命令

问题描述

我想用python在Windows上调用一个exe。如此调用的exe文件在内部进行处理,然后提示输入,一旦输入,则提示输入其他内容。因此,我想将提示输入保留在python列表中,然后调用exe。等待提示出现,然后在列表中提供第一个字符串,然后在第二个提示中提供列表中的第二个字符串。基本上,我想在python中创建一个函数,使其能够像Windows上的期望一样。

尝试了下面提供的以下代码Interact with a Windows console application via Python,但是对于Windows 10似乎不再起作用:

CREATE TABLE IF NOT EXISTS[Category] (CategoryId INTEGER PRIMARY KEY
                    AUTOINCREMENT,CategoryName varchar(100) not null)

CREATE TABLE IF NOT EXISTS[Producer] (ProducerId INTEGER PRIMARY KEY
                    AUTOINCREMENT,ProducerName varchar(100) not null)

有人可以帮忙吗?

解决方法

子进程程序包在Windows 10上正常运行。请尝试以下命令。

import subprocess

p = subprocess.Popen('dir',shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
out,err = p.communicate()
print(out.decode('utf-8'))

更新 您的代码在Python 2.7中运行良好。问题似乎出在Python 3上。

enter image description here