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

Danny Yoo dyoo at hashcollision.org
Mon Feb 16 23:11:06 CET 2015


On Mon, Feb 16, 2015 at 1:26 PM, Ken G. <beachkidken at gmail.com> wrote:
> Wow, just found out this morning that the following
> terms of:
>
> import os
> pr = os.popen("lpr", "w")
> pr.write(month), pr.write(" "),
> pr.write("\t\tLine ")
>
> was deprecated. In place there of, there is
> a subprocess to use.

Hi Ken,


Yes: subprocess.Popen(), along with Popen.communicate(), are what you
want to look at.

For your example above, the change is relatively direct:

##############################################
import subprocess
pr = subprocess.Popen(['lpr', 'w'])
pr.communicate(month + ' ' + '\t\tLine ')
##############################################

The main difference between this and what you had earlier is that you
send all the input at once using Popen.communicate().

There's a quick-and-dirty section in the documentation for converting
from the os.popen call to the subprocess equivalents.  See:

https://docs.python.org/2/library/subprocess.html#replacing-os-popen-os-popen2-os-popen3


More information about the Tutor mailing list