【发布时间】:2022-01-04 13:26:25
【问题描述】:
我正在使用 subprocess.run 中的输入使用 python 子进程传递用户输入
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
然后等待一段时间,然后请求输入 确认操作:(是/否)
不确定如何使子进程不通过管道发送yes 输入,而是等待提示并检查此行Confirm operation: (yes/no),然后将输入提供为yes 或no
我相信我当前的代码可以执行
yes | java -jar cli.jar ion -r DUB -n oa -cf /config/path -e test -c ht -f true cli 不例外的那种操作。
任何关于如何实现这一点的帮助都会很棒
【问题讨论】:
-
您可以尝试使用来自stackoverflow.com/a/48787335/7916438 的部分,它展示了如何将输入发送到
subprocess。我不确定输出部分,在终端中可能根本不需要重定向它,它只会出现。 -
@tevemadar 据我所知,提供 input="whatever" 有效吗?并且您提供的参考是 subprocess.popen
标签: python python-3.x python-2.7 subprocess