title: How to run two adb shell commands via Python, but store output of only one?
body: <p>I am [new to] using Python 2.7 on Windows 7 and I am trying to incorporate adb shell commands to access/change the directories on my Android device. What I need to do is get the size of a directory in Internal Storage, save that output as a variable, and then delete the directory. From my research I believe I should be using <code>subprocess.Popen()</code>to create the shell and then <code>.communicate()</code> to send the required commands. However I am currently only able to execute one of the commands: delete the directory. Below is the code that I used:</p>
  
  <pre><code> import os, subprocess
   from subprocess import PIPE, Popen
  
   adb_shell = subprocess.Popen('adb shell', stdin = subprocess.PIPE)
   adb_shell.communicate('cd /sdcard\nrm -r "Directory to Delete"\nexit\n')
  </code></pre>
  
  <p>However, if I add another command by doing:</p>
  
  <pre><code>adb_shell.communicate('cd /sdcard\ndu -sh "Directory A"\nrm -r "Directory A"\nexit\n')
  </code></pre>
  
  <p>It does not work because I need to incorporate <code>stdout = subprocess.PIPE</code> to store the output of the <code>du -sh "Directory A"</code>command, but how do I go about doing that? If I add it like: <code>adb_shell = subprocess.Popen('adb shell', stdin = subprocess.PIPE, stdout = subprocess.PIPE)</code>, it does not work. Any suggestions? ty!</p>
  
  <p>Edit: The closest I've come to a solution (actually getting an output in the interpreter and removing the file afterwards) is with:</p>
  
  <pre><code>    adb_shell = subprocess.Popen('adb shell', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
      adb_shell.communicate('cd /sdcard\ndu -sh "qpython"\nexit\n')
      output = adb_shell.stdout
      print output
  
      adb_shell = subprocess.Popen('adb shell', stdin = subprocess.PIPE)
      adb_shell.communicate('cd /sdcard\nrm -r "qpython"\nexit\n')
  </code></pre>
  
  <p>Which has an output of: '', mode 'rb' at 0x02B4D910>'</p>