[Tutor] Popen was deprecated since Python 2.6, now what?

Alan Gauld alan.gauld at btinternet.com
Tue Feb 17 00:26:12 CET 2015


On 16/02/15 21:26, Ken G. wrote:
> I have not been able to figure out on how to use a
> subprocess in place of my former popen. I have been
> reading the new materials on the subprocess  all day
> and it is still not quite understandable.


Here is what the docs say:

###########################################################
17.5.5.5. Replacing os.popen(), os.popen2(), os.popen3()

(child_stdin, child_stdout) = os.popen2(cmd, mode, bufsize)
==>
p = Popen(cmd, shell=True, bufsize=bufsize,
           stdin=PIPE, stdout=PIPE, close_fds=True)
(child_stdin, child_stdout) = (p.stdin, p.stdout)




(child_stdin,
  child_stdout,
  child_stderr) = os.popen3(cmd, mode, bufsize)
==>
p = Popen(cmd, shell=True, bufsize=bufsize,
           stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
(child_stdin,
  child_stdout,
  child_stderr) = (p.stdin, p.stdout, p.stderr)




(child_stdin, child_stdout_and_stderr) = os.popen4(cmd, mode, bufsize)
==>
p = Popen(cmd, shell=True, bufsize=bufsize,
           stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
(child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout)





Return code handling translates as follows:

pipe = os.popen(cmd, 'w')
...
rc = pipe.close()
if rc is not None and rc >> 8:
     print("There were some errors")
==>
process = Popen(cmd, 'w', stdin=PIPE)
...
process.stdin.close()
if process.wait() != 0:
     print("There were some errors")



17.5.5.6. Replacing functions from the popen2 module

Note
If the cmd argument to popen2 functions is a string, the command is 
executed through /bin/sh. If it is a list, the command is directly executed.



(child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode)
==>
p = Popen("somestring", shell=True, bufsize=bufsize,
           stdin=PIPE, stdout=PIPE, close_fds=True)
(child_stdout, child_stdin) = (p.stdout, p.stdin)



(child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, 
mode)
==>
p = Popen(["mycmd", "myarg"], bufsize=bufsize,
           stdin=PIPE, stdout=PIPE, close_fds=True)
(child_stdout, child_stdin) = (p.stdout, p.stdin)


popen2.Popen3 and popen2.Popen4 basically work as subprocess.Popen, 
except that:
     Popen raises an exception if the execution fails.
     the capturestderr argument is replaced with the stderr argument.
     stdin=PIPE and stdout=PIPE must be specified.
     popen2 closes all file descriptors by default,
     but you have to specify close_fds=True with Popen
     to guarantee this behavior on all platforms or past Python versions.
#############################################################

Which bits of that don't you understand?
That way we have something concrete to work with.

For example, Your popen program:

import os
pr = os.popen("lpr", "w")
pr.write(month), pr.write(" "),
pr.write("\t\tLine ")

Translates according to the above as

import subprocess as sub
pr = sub.Popen(["lpr"], stdin = sub.PIPE)
pr.stdin.write(month)
pr.stdin.write(" ")
pr.stdin.close()
if pr.wait() != 0: print 'Errors!'

Now, how can we help further clarify things?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list