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

Ken G. beachkidken at gmail.com
Tue Feb 17 01:10:56 CET 2015


On 02/16/2015 05:11 PM, Danny Yoo wrote:
> 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
> .
Thank you for helping me get on the road here. I printed out your 
response and I'll be playing with it.

Ken



More information about the Tutor mailing list