How to create a text file pager in Python? I.e. a rudimentary version of a program like the more or less commands of Unix?
Asked
Active
Viewed 465 times
0
-
I'm posting a question and answering it myself for two reasons: 1) to share the code with others, and 2) so that others can comment on my code, to help me improve it. Also, see this post by Jeff Atwood, co-founder of StackOverflow: https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/ – Vasudev Ram Apr 13 '17 at 19:34
-
you have more in windows an linux so why reinventing the wheel? – yossico Apr 13 '17 at 19:34
-
@yossico: I should have expected this kind of question, so should have mentioned it up front: this is for learning purposes, not for reinventing the wheel. See my comment. I'm going to answer my question too. – Vasudev Ram Apr 13 '17 at 19:36
-
1Great that you help making the internet a better place, +1 4u – yossico Apr 13 '17 at 19:38
-
@yossico: Thanks. Another benefit is that such tools can be cross-platform to any platform where Python runs. No need to install cygwin or similar. Of course, I am not trying to replicate the full functionality of less or more here - at least less has a lot more - ha ha, pun intended. This is more like a demo to show the core concepts involved in a pager - beginner stuff. – Vasudev Ram Apr 13 '17 at 19:48
-
I appreciate the idea of posting this but I don't think posting a far too broad question and answering with only the code from [here](https://jugad2.blogspot.ca/2017/02/tp-simple-text-pager-in-python.html) without any extra stuff of your own is the right way to do it. – Tadhg McDonald-Jensen Apr 13 '17 at 20:03
-
@TadhgMcDonald-Jensen: The answer is taken from my own blog post, which I wrote some days before posting both the question and the answer here today. Also, see my first comment to my own question, in which I have linked to SO founder Jeff Atwood's post about it being perfectly okay and even encouraged to ask and then answer one's own questions. – Vasudev Ram Apr 13 '17 at 20:15
-
I understand what you are trying to do and have seen that post and have [this question](http://stackoverflow.com/questions/36250353/importing-installed-package-from-script-raises-attributeerror-module-has-no-at) bookmarked as I use it frequently enough as a duplicate target. But I still think both the question and answer are fairly low quality as far as SO posts go. I'd wonder if this'd be more suited for [SO's docs](http://stackoverflow.com/documentation/python/topics) as an example.. – Tadhg McDonald-Jensen Apr 13 '17 at 20:21
-
Your link labelled "this question" (title starts with: Importing installed package from script raises “AttributeError"), seems to have nothing at all to do with my answer. Not sure what you mean, or if you made some typo in the link. And I don't agree that the Q and A are of low quality, or at least, if you say so, you should justify your comment. Also I clearly said it is for beginners, in my first or second comment on the question itself. I suggest you not be in such haste to put others down without doing your homework first. – Vasudev Ram Apr 13 '17 at 20:26
-
Let me start again, I appreciate that you are trying to make resources available for new programmers. However the question **on it's own** is just asking for working code which is discouraged on SO. And the answer **on it's own** is just a block of code without comments or explanation, I do see the details at your blog but it should be in the answer here or people will miss it. – Tadhg McDonald-Jensen Apr 13 '17 at 22:12
-
I'd wonder if posting your code on [the doc's page on file io](I think posting it [on the file/io examples page](http://stackoverflow.com/documentation/python/267/files-folders-i-o/982/getting-the-full-contents-of-a-file#t=201704132209466257445) would be a better fit, it'd be more likely to be seen by people looking for examples of file io etc. – Tadhg McDonald-Jensen Apr 13 '17 at 22:13
-
>However the question on it's own is just asking for working code which is discouraged on SO. And the answer on it's own is just a block of code without comments or explanation, I do see the details at your blog but it should be in the answer here or people will miss it. I did not know that that was discouraged on SO. Also, I initially thought of putting more explanation here (about the code) but then thought it would be redundant, since I could link to my blog post, which has that. Will consider putting this on the file I/O page on the docs section of SO. – Vasudev Ram Apr 13 '17 at 22:52
1 Answers
1
'''
tp.py
Purpose: A simple text pager.
Version: 0.1
Platform: Windows-only.
Can be adapted for Unix using tty / termios calls.
Only the use of msvcrt.getch() needs to be changed.
Author: Vasudev Ram
Copyright 2017 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: https://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram
'''
import sys
import string
from msvcrt import getch
def pager(in_fil=sys.stdin, lines_per_page=10, quit_key='q'):
assert lines_per_page > 1 and lines_per_page == int(lines_per_page)
assert len(quit_key) == 1 and \
quit_key in (string.ascii_letters + string.digits)
lin_ctr = 0
for lin in in_fil:
sys.stdout.write(lin)
lin_ctr += 1
if lin_ctr >= lines_per_page:
c = getch().lower()
if c == quit_key.lower():
break
else:
lin_ctr = 0
def main():
try:
sa, lsa = sys.argv, len(sys.argv)
if lsa == 1:
pager()
elif lsa == 2:
with open(sa[1], "r") as in_fil:
pager(in_fil)
else:
sys.stderr.write
("Only one input file allowed in this version")
except IOError as ioe:
sys.stderr.write("Caught IOError: {}".format(repr(ioe)))
sys.exit(1)
except Exception as e:
sys.stderr.write("Caught Exception: {}".format(repr(e)))
sys.exit(1)
if __name__ == '__main__':
main()
More details here:
https://jugad2.blogspot.in/2017/02/tp-simple-text-pager-in-python.html

Vasudev Ram
- 145
- 1
- 7
-
@TadhgMcDonald-Jensen Isn't it taken from the OP's own blog though? – EJoshuaS - Stand with Ukraine Apr 13 '17 at 20:07
-
@EJoshuaS yes it seems it is... Well I guess I should have looked for that first huh. – Tadhg McDonald-Jensen Apr 13 '17 at 20:09
-
@TadhgMcDonald-Jensen: You flagged incorrectly, and too soon, without checking the source. The answer is taken from my own blog. I'm Vasudev Ram and jugad2.blogspot.com is my blog. – Vasudev Ram Apr 13 '17 at 20:17