Stdout / stderr strings or string output

I have code, this is the standard command for stdout.

params = [toolsDir + "\\adb.exe", "shell", "pm", "path", app]
p = Popen(params, shell=False, stdout=subprocess.PIPE)
stdout, stderr = p.communicate()
    if "package:" in stdout:
         package = stdout[8:].rstrip()

Line 3 returns the line, since line 5 works successfully without errors, I can split rstrip () into a "package".

 stdout, stderr = p.communicate()

However ... if I remove 'stderr' from line 3, then it ends as

stdout = p.communicate() 

I get an error message:

    package = stdout[8:].rstrip()
AttributeError: 'tuple' object has no attribute 'rstrip'

Can someone explain why this happens, since stderr is not even defined for the channel in line 2 of Popen, so why does it return a tuple without stderr, but a line with it?

I have already fixed this problem, although it took me 30 minutes, and now I want to know why this matters. Thanks.

+4
source share
2 answers

communicate returns a tuple

, ( ):

stdout, stderr = p.communicate()

(, ):

stdout = p.communicate() # tuple with 'stdout' and 'stderr'

stderr, :

stdout = p.communicate()[0]
+3

, :

>>> a, b = (1,2)
>>> a
1
>>> b
2

b , :

>>> a = (1,2)
>>> a
(1, 2)

a , , .

+1

Source: https://habr.com/ru/post/1605905/


All Articles