title: Print and pexpect logging
body: <p>I have a bit of a code that uses <code>pexpect</code> to control a process and some prints in the code. The main goal (in this question) is to have the <code>pexpect</code> output and prints logged into some log file. The problem I ran into is that the <code>pexpect</code> lines (data sent and received) is mingled with the prints with no apparent logic. I was expecting that the print strings and <code>pexpect</code> outputs will be logged in the order they were issued.</p>
  
  <p>Sample code is the following:</p>
  
  <pre><code>#!/usr/bin/env python
  
  import pexpect
  import time, sys, os
  
  ###############################################################################
  # Subclass of file object to avoid recording extensive whitespace characters
  class CleanFile(file):
      def write (self, text):
          # Remove the whitespaces
          out_text = ''
          # process the backspace properly
          bline = ''
          for c in text:
              if (ord(c) == 0x8):
                  if (len(bline) == 0):
                      # Move the file pointer.
                      file.seek(self, -1, os.SEEK_CUR);
                  else:
                      bline = bline[:-1]
              else:
                  bline += c
  
          # remove whitespaces from inside a line
          out_text += ''.join(c for c in bline if (ord(c) &gt;= 32 or ord(c) == 10));
  
          file.write(self, out_text);
  
  ###############################################################################
  def main():
      fout = CleanFile ("options.log_file.log", 'w')
  
      sys.stdout = os.fdopen (sys.stdout.fileno(), 'w', 0)
      os.dup2 (fout.fileno(), sys.stdout.fileno());
  
      p = pexpect.spawn ('tclsh')
      p.logfile = fout
  
      print "Got into tclsh."
      p.sendline('ls');
      p.expect (['%',pexpect.EOF])
  
      p.sendline('info tclversion');
      p.expect (['%',pexpect.EOF])
  
      print "Got the version\n"
  
      p.sendline('info commands %');
      p.expect (['%',pexpect.EOF])
  
      p.sendline('exit');
  
      print 'Ended session'
  
  ###############################################################################
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>This is the output log file contents:</p>
  
  <pre><code>Got into tclsh.
  ls
  % lsinfo tclversion
  
  log  options.log_file.log  pexpect_test.py  runtests.py  runtests_steinway.py
  % info tclversionGot the version
  
  info commands %
  
  8.4
  % info commands %exit
  Ended session
  </code></pre>
  
  <p>Is there any way to make the <code>pexpect</code> and print outputs sequential?</p>
  
  <hr>
  
  <p><em>Update</em>: Based on the <code>pexpect</code> <a href="http://pexpect.sourceforge.net/pexpect.html#spawn-expect" rel="noreferrer">manual page</a>: "Please note, however, that buffering can affect this behavior, since
  input arrives in unpredictable chunks". So it may potentially affect the logging.</p>