![](/img/trans.png)
[英]Python subprocess to execute external Command prompt scripts, trying to run one .exe file by passing file location
[英]passing subprocess run prompt input in python
我正在使用 python 子进程使用 subprocess.run 中的输入传递用户输入
dargs = ['java','-jar','cli.jar','ion','-cf', config_path, '-r', deldata.get("reg"), '-n', deldata.get("space"), '-e', deldata.get("tion"), '-c', listargs[2], '-f', 'true']
dresp = subprocess.run(dargs, text=True, capture_output=True, input="yes")
这将返回以下错误:
returned non-zero exit status 1.
When calling: subprocess.run(dargs, text=True, capture_output=True, check=True, input="yes")
但如果我从终端做同样的事情,它工作正常
java -jar cli.jar ion -r DUB -n oa -cf /config/path -e test -c ht -f true
上面的命令输出以下内容并要求在终端中输入这样的用户输入
this is force operation.
V Id: sl, Desc: ht
Confirm operation: (yes/no)yes #----->here comes my user input to terminal
Operation done successfully
从终端触发的命令首先输出这两行
this is force operation.
V Id: sl, Desc: ht
然后等待一段时间,然后要求输入确认操作:(是/否)
不确定如何让子进程等待并检查此行Confirm operation: (yes/no)
,然后将输入提供为yes
或no
注意:在打印标准输出时注意到它显示<_io.BufferedReader name=7>
尝试在输入参数( input="yes\r", ...
)中的字符串末尾传递一个\r
。 此外,这取决于“cli.jar”与 Python 脚本位于同一文件夹中,并从该文件夹调用 Python 脚本。 将完整的绝对路径添加到“cli.jar”将有所帮助。
有趣的是,如果没有将“check=True”传递给 subprocess.run,程序不应该中断:你只会在你的dresp
程序上获得失败的运行信息,你可以打印它来获取你尝试的程序的任何信息运行打印出来(由于 capture_output=True)。
如果它不起作用,请尝试分步解决您的问题,例如尝试先运行java -version
,这样您就可以断言 java 解释器正在定位并成功运行。 此外,如果您的程序在调用时没有中断,只需在此处添加一个print(dresp)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.