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
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 13.04, Mint 11, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1096
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: 285
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests