title: Paging output from python
body: <p>I'm trying to implement something similar to <code>git log</code>, which will only page the output if the log is of a certain length.  If you're not familiar with git, I'm essentially trying to achieve this:</p>
  
  <pre><code>python some_script.py | less
  </code></pre>
  
  <p>With some help from the paging implementation in python2.6/pydoc.py, I was able to come up with this:</p>
  
  <pre><code>import os
  text = '...some text...'
  pipe = os.popen('less', 'w')
  pipe.write(text)
  pipe.close()
  </code></pre>
  
  <p>which works great, but os.popen() is deprecated. I've considered writing to a temp file and calling less with its path, but that doesn't seem ideal. Is this possible with subprocess? Any other ideas?</p>
  
  <p>EDIT:</p>
  
  <p>So I've gotten subprocess working. I was able to give it the text variable with <code>Popen.communicate(text)</code>, but since I really want to redirect print statements, I've settled on this:</p>
  
  <pre><code>  import os, sys, subprocess, tempfile
  
    page = True
    if page:
        path = tempfile.mkstemp()[1]
        tmp_file = open(path, 'a')
        sys.stdout = tmp_file
    print '...some text...'
    if page:
        tmp_file.flush()
        tmp_file.close()
        p = subprocess.Popen(['less', path], stdin=subprocess.PIPE)
        p.communicate()
        sys.stdout = sys.__stdout__     
  </code></pre>
  
  <p>Of course, I'd end up wrapping it into functions.  Does anyone see a problem with that?</p>