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

J

johnny

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]

Thank you.
 
F

Fredrik Lundh

johnny said:
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.

</F>
 
N

Neil Cerutti

johnny said:
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()
 
G

Gabriel Genellina

I like using pattern matching in these simple cases:

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

pattern matching???


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top