Define a Unix-like tail command using Python

Usage: python tail.py [OPTION] [FILE]

Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.

Mandatory arguments to long options are mandatory for short options too:
-h, --help display this help and exit
-n, --lines=K output the last K lines, instead of the last 10; or use -n +K to output starting with the Kth line

Note: when executed in Unix-like system, users can run as the unix command style:

$ mv tail.py tail
$ chmod +x tail
$ ./tail [OPTION] [FILE]

See also: Github

demo 1: run with -h | --help option


In [1]:
%run tail.py -h


Define a Unix-like tail command.

Usage: python tail.py [OPTION] <FILE>
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.

Mandatory arguments to long options are mandatory for short options too.
-h, --help    display this help and exit
-n, --lines=K    output the last K lines, instead of the last 10;
                or use -n +K to output starting with the Kth line

-----------------------------------
Created on Mon Jan 25, 2016
@author: mangwang
-----------------------------------


In [2]:
%run tail.py --help


Define a Unix-like tail command.

Usage: python tail.py [OPTION] <FILE>
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.

Mandatory arguments to long options are mandatory for short options too.
-h, --help    display this help and exit
-n, --lines=K    output the last K lines, instead of the last 10;
                or use -n +K to output starting with the Kth line

-----------------------------------
Created on Mon Jan 25, 2016
@author: mangwang
-----------------------------------

demo 2: run with no options


In [3]:
%run tail.py shakespeare.txt


"When sorrows come, they come not single spies, but in battalions". - (Act IV, Scene V).
As You Like It
"All the world 's a stage, and all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts" - (Act II, Scene VII).
"Can one desire too much of a good thing?". - (Act IV, Scene I).
"I like this place and willingly could waste my time in it" - (Act II, Scene IV).
"How bitter a thing it is to look into happiness through another man's eyes!" - (Act V, Scene II).
"Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude".(Act II, Scene VII).
"True is it that we have seen better days". - (Act II, Scene VII).
"For ever and a day". - (Act IV, Scene I).
"The fool doth think he is wise, but the wise man knows himself to be a fool". - (Act V, Scene I).

demo 3: run with -n | --lines K options


In [4]:
%run tail.py -n 5 shakespeare.txt


"How bitter a thing it is to look into happiness through another man's eyes!" - (Act V, Scene II).
"Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude".(Act II, Scene VII).
"True is it that we have seen better days". - (Act II, Scene VII).
"For ever and a day". - (Act IV, Scene I).
"The fool doth think he is wise, but the wise man knows himself to be a fool". - (Act V, Scene I).

In [5]:
%run tail.py --lines 5 shakespeare.txt


"How bitter a thing it is to look into happiness through another man's eyes!" - (Act V, Scene II).
"Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude".(Act II, Scene VII).
"True is it that we have seen better days". - (Act II, Scene VII).
"For ever and a day". - (Act IV, Scene I).
"The fool doth think he is wise, but the wise man knows himself to be a fool". - (Act V, Scene I).

demo 4: run with -n | --lines +K options


In [6]:
%run tail.py -n +20 shakespeare.txt


As You Like It
"All the world 's a stage, and all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts" - (Act II, Scene VII).
"Can one desire too much of a good thing?". - (Act IV, Scene I).
"I like this place and willingly could waste my time in it" - (Act II, Scene IV).
"How bitter a thing it is to look into happiness through another man's eyes!" - (Act V, Scene II).
"Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude".(Act II, Scene VII).
"True is it that we have seen better days". - (Act II, Scene VII).
"For ever and a day". - (Act IV, Scene I).
"The fool doth think he is wise, but the wise man knows himself to be a fool". - (Act V, Scene I).

In [7]:
%run tail.py --lines +20 shakespeare.txt


As You Like It
"All the world 's a stage, and all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts" - (Act II, Scene VII).
"Can one desire too much of a good thing?". - (Act IV, Scene I).
"I like this place and willingly could waste my time in it" - (Act II, Scene IV).
"How bitter a thing it is to look into happiness through another man's eyes!" - (Act V, Scene II).
"Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude".(Act II, Scene VII).
"True is it that we have seen better days". - (Act II, Scene VII).
"For ever and a day". - (Act IV, Scene I).
"The fool doth think he is wise, but the wise man knows himself to be a fool". - (Act V, Scene I).

demo 5: run multiple files


In [8]:
%run tail.py shakespeare.txt she.txt


==> shakespeare.txt <==
"When sorrows come, they come not single spies, but in battalions". - (Act IV, Scene V).
As You Like It
"All the world 's a stage, and all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts" - (Act II, Scene VII).
"Can one desire too much of a good thing?". - (Act IV, Scene I).
"I like this place and willingly could waste my time in it" - (Act II, Scene IV).
"How bitter a thing it is to look into happiness through another man's eyes!" - (Act V, Scene II).
"Blow, blow, thou winter wind! Thou art not so unkind as man's ingratitude".(Act II, Scene VII).
"True is it that we have seen better days". - (Act II, Scene VII).
"For ever and a day". - (Act IV, Scene I).
"The fool doth think he is wise, but the wise man knows himself to be a fool". - (Act V, Scene I).
==> she.txt <==
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!