title: Unable to run subprocess and also redirecting output to a log file
body: <p>The following code does not write into the log file which I am creating (Dm_Log.txt). But if I comment out stdout,stderr = process.communicate() then it does. If I do not use communicate, subprocess stalls since I am doing process.wait(). How to resolve this? I need to write in the log file and run the process too.</p>
  
  <pre><code>  logfilePath = self.psexeclogs + 'Dm_Log.txt'
    logfile = file(logfilePath,'w')
  
    try:
  
     process = subprocess.Popen(drivemaster_open_command, stdout = subprocess.PIPE,stderr = subprocess.PIPE)
     stdout, stderr = process.communicate()
  
     for line in process.stderr:
      print ' '
      sys.stderr.write(line)
      logfile.write(line)
     process.wait()
  
    except OSError:
      print "********COULD NOT FIND PSEXEC.EXE, PLEASE REINSTALL AND SET THE PATH VARIABLE PROPERLY********\n"
  
    #Close the logfile first
    logfile.close()
  </code></pre>