differences between subprocess Popen and check_ouput

This is the place for queries that don't fit in any of the other categories.

differences between subprocess Popen and check_ouput

Postby metulburr » Sat May 25, 2013 3:49 pm

What is exactly the difference between subprocess.Popen and subprocess.check_output?
return status 0
Code: Select all
import subprocess
cmd = 'ls -l'

proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
stringer = proc.communicate()[0].decode().strip()
print(stringer)


proc = subprocess.check_output(cmd.split())
print(proc.decode())


All i see here is check_output returns subprocess.CalledProcessError upon the command not returning zero status. This would look ideal, but then you cannot pipe the output in check_output as you can in Popen.
Code: Select all
import subprocess
cmd = 'ls non_existing_file.txter'

proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
stringer = proc.communicate()[0].decode().strip()
print(stringer)


proc = subprocess.check_output(cmd.split())
print(proc.decode())


so if you were planning on piping the output, you would first check_output for a subprocess.CalledProcessError, and if success, you would recall the command in Popen? Thats seems redundant.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1455
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: differences between subprocess Popen and check_ouput

Postby setrofim » Sat May 25, 2013 5:45 pm

check_output is just a convenience function -- it's a wrapper around Popen.

metulburr wrote:so if you were planning on piping the output, you would first check_output for a subprocess.CalledProcessError, and if success, you would recall the command in Popen?

Nope, you would just use Popen and check the returncode attribute of the Popen object.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: 7stud, Baldyr, rjnicholson and 3 guests