python - whats the difference between .communicate() and .communicate()[0]? -
i'm using python. i'm trying run process , fetch output using subprocess.popen. after reading around see people using communicate()[0] thats not in docs, used in example. i'm wondering if fill me in why 1 append [0] communicate()?
from docs:
popen.communicate(input=none) interact process: send data stdin. read data stdout , stderr, until end-of-file reached. wait process terminate. optional input argument should string sent child process, or none, if no data should sent child.
communicate() returns tuple (stdoutdata, stderrdata).
so communicate()[0] used access stdoutdata , communicate()[1] access stderrdata
to access elements in tuple or list use indexes, first element @ index 0.
>>> tup = ("foo", "bar") >>> tup[0] 'foo' >>> tup[1] 'bar'
Comments
Post a Comment