[英]Python3 subprocess check_output returns empty for some commands
I am a python3 beginner. 我是python3初学者。 I am trying to get the java version with a python3 script. 我正在尝试使用python3脚本获取Java版本。 After I checked the docs, I saw that subprocess.check_output might be what I need. 在检查了文档之后,我看到了subprocess.check_output可能正是我所需要的。
output = subprocess.check_output(["java", "-version"])
print("Output is {}".format(output))
The problem is the output I am getting is 问题是我得到的输出是
Output is b''
Why am I not getting the correct string that I get with bash? 为什么我没有得到bash正确的字符串?
Thanks 谢谢
For some reason your output lands in the stderr
. 由于某种原因,您的输出将stderr
。 You can pipe that to the return value like this: 您可以像这样通过管道将其传递给返回值:
output = subprocess.check_output(["java", "-version"], stderr=subprocess.STDOUT)
If somebody knows why it goes to stderr
, I'd be happy to hear it. 如果有人知道为什么stderr
,我很高兴听到它。 ["python", "--version"]
, for example, works as expected. 例如, ["python", "--version"]
可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.