Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > putting text through pager

Reply
Thread Tools

putting text through pager

 
 
Michael Goerz
Guest
Posts: n/a
 
      03-20-2008
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
 
Reply With Quote
 
 
 
 
Michael Goerz
Guest
Posts: n/a
 
      03-20-2008
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Piping println() through an external pager program (more, less) Thomas Kellerer Java 47 04-12-2009 08:45 PM
[ANN] FastRI 0.2.1: faster full-text searching, multiple queries, pager, class/method lists Mauricio Fernandez Ruby 5 11-23-2006 05:23 PM
Difference between putting code in constructor and putting code in static{} Saurabh Java 6 05-30-2004 02:44 PM
javascript for putting text in an asp.net edit control Michelle Stone ASP .Net 1 10-15-2003 10:44 AM
DataGrid Pager Text? szabelin ASP .Net Building Controls 1 09-09-2003 07:08 PM



Advertisments