简体   繁体   中英

Python3 subprocess check_output returns empty for some commands

I am a python3 beginner. I am trying to get the java version with a python3 script. After I checked the docs, I saw that subprocess.check_output might be what I need.

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?

Thanks

For some reason your output lands in the 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. ["python", "--version"] , for example, works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM