WHAT is [0] in subprocess.Popen(blah).communicate()[0]

Neil Cerutti horpner at yahoo.com
Thu Dec 14 13:30:37 EST 2006


On 2006-12-14, Fredrik Lundh <fredrik at pythonware.com> wrote:
> johnny wrote:
>
>> Can someone tell me what is the reason "[0]" appears after
>> ".communicate()"
>> 
>> For example:
>> last_line=subprocess.Popen([r"tail","-n 1", "x.txt"],
>> stdout=subprocess.PIPE).communicate()[0]
>
> as explained in the documentation, communication() returns two
> values, as a tuple.  [0] is used to pick only the first one.
>
> see the Python tutorial for more on indexing and slicing.

I like using pattern matching in these simple cases:

  last_line, _ = subprocess.Popen([r"tail","-n 1", "x.txt"],
          stdout=subprocess.PIPE).communicate()

-- 
Neil Cerutti



More information about the Python-list mailing list