putting text through pager

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Goerz

    putting text through pager

    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_pag er(displaystrin g):
    less_pipe = subprocess.Pope n(\
    'less', shell=True, \
    stdin=subproces s.PIPE).stdin
    less_pipe.write (displaystring)
    less_pipe.close ()

    def main():
    put_through_pag er(longstring)


    longstring = """
    Lorem ipsum dolor sit amet,...
    Reference site about Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.

    """

    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

    #2
    Re: putting text through pager

    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_pag er(displaystrin g):
    less_pipe = subprocess.Pope n(\
    'less', shell=True, \
    stdin=subproces s.PIPE).stdin
    less_pipe.write (displaystring)
    less_pipe.close ()
    >
    def main():
    put_through_pag er(longstring)
    >
    >
    longstring = """
    Lorem ipsum dolor sit amet,...
    Reference site about Lorem Ipsum, giving information on its origins, as well as a random Lipsum generator.

    """
    >
    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_pag er(displaystrin g):
    (temp_fd, tempname) = tempfile.mkstem p(".mail")
    temp_fh = os.fdopen(temp_ fd, "w")
    temp_fh.write(d isplaystring)
    temp_fh.close()
    os.system("less %s" % tempname)
    os.unlink(tempn ame)

    Comment

    Working...