简体   繁体   English

Python3子进程check_output对于某些命令返回空

[英]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.

相关问题 Python子进程check_output解码特殊字符 - Python subprocess check_output decoding specials characters 子进程python 3 check_output与shell命令不同吗? - subprocess python 3 check_output not same as shell command? 当我从批处理文件和任务计划程序运行脚本时,子进程运行、check_output、Popen 返回空字符串 - Subprocess run, check_output, Popen returns empty string when I run the script from a batch file and from Task Scheduler Python子进程check_output FileNotFoundError:[Errno 2]没有这样的文件或目录 - Python subprocess check_output FileNotFoundError: [Errno 2] No such file or directory 子进程check_output不适用于SetFile(Mac) - Subprocess check_output not working with SetFile (Mac) 无法从子流程check_output检索输出 - Can't retrieve output from subprocess check_output 显示子流程check_output的可读输出 - Display readable output from subprocess check_output 使用 mediainfo 在 python3 中运行 check_output 会产生错误,即使终端中的相同调用工作正常 - Running a check_output in python3 with mediainfo produces an error, even though the same call in terminal works fine python3子进程错误从shell命令捕获output - python3 subprocess error capturing output from shell commands Python3子进程输出 - Python3 subprocess output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM