putting text through pager

Discussion in 'Python' started by Michael Goerz, Mar 20, 2008.

  1. Hi,

    I'm trying to print some variable through a pager (i.e. 'less') on a
    linux system. My attempt was this:


    ====== snip here ======
    import subprocess

    def put_through_pager(displaystring):
    less_pipe = subprocess.Popen(\
    'less', shell=True, \
    stdin=subprocess.PIPE).stdin
    less_pipe.write(displaystring)
    less_pipe.close()

    def main():
    put_through_pager(longstring)


    longstring = """
    Lorem ipsum dolor sit amet,...
    http://www.lipsum.com/
    """

    main()

    ====== snip here ======

    That doesn't work however: first of all, it only flashes the text for a
    fraction of a second, and secondly, after I run the program my terminal
    is broken, not echoing whatever I type back to me.

    Any suggestions for putting text through a pager from Python? This is
    strictly on a linux system, of course.

    Thanks,
    Michael
    Michael Goerz, Mar 20, 2008
    #1
    1. Advertising

  2. Michael Goerz wrote, on 03/20/2008 04:43 PM:
    > Hi,
    >
    > I'm trying to print some variable through a pager (i.e. 'less') on a
    > linux system. My attempt was this:
    >
    >
    > ====== snip here ======
    > import subprocess
    >
    > def put_through_pager(displaystring):
    > less_pipe = subprocess.Popen(\
    > 'less', shell=True, \
    > stdin=subprocess.PIPE).stdin
    > less_pipe.write(displaystring)
    > less_pipe.close()
    >
    > def main():
    > put_through_pager(longstring)
    >
    >
    > longstring = """
    > Lorem ipsum dolor sit amet,...
    > http://www.lipsum.com/
    > """
    >
    > main()
    >
    > ====== snip here ======
    >
    > That doesn't work however: first of all, it only flashes the text for a
    > fraction of a second, and secondly, after I run the program my terminal
    > is broken, not echoing whatever I type back to me.
    >
    > Any suggestions for putting text through a pager from Python? This is
    > strictly on a linux system, of course.
    >
    > Thanks,
    > Michael

    Using a tempfile seems to be a good solution:

    def put_through_pager(displaystring):
    (temp_fd, tempname) = tempfile.mkstemp(".mail")
    temp_fh = os.fdopen(temp_fd, "w")
    temp_fh.write(displaystring)
    temp_fh.close()
    os.system("less %s" % tempname)
    os.unlink(tempname)
    Michael Goerz, Mar 20, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michelle Stone
    Replies:
    1
    Views:
    559
    Emilio
    Oct 15, 2003
  2. Saurabh
    Replies:
    6
    Views:
    4,527
    Chris Smith
    May 30, 2004
  3. Thomas Kellerer
    Replies:
    47
    Views:
    1,505
    Martin Gregorie
    Apr 12, 2009
  4. szabelin

    DataGrid Pager Text?

    szabelin, Sep 9, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    133
    Matt Hawley
    Sep 9, 2003
  5. Mauricio Fernandez
    Replies:
    5
    Views:
    112
    Robert Feldt
    Nov 23, 2006
Loading...

Share This Page