Implementing a head/tail command for iPython/Jupyter on Windows

Monday, September 11, 2017

Ever wished that you could use the head or tail command (available on Linux and Mac OS X) in iPython or the Jupyter notebook when using WIndows?

Here's how I implemented a head and tail command for iPython and Jupyter on Windows. It depends on the following Powershell 3.0 (Windows 8 and higher) commands :

 Get-Content filename -Head n

 Get-Content filename -Tail n

SInce we will be using iPython shell access, we have to run it from DOS as follows :

 powershell -command "& {Get-Content filename -Head 10}"

 powershell -command "& {Get-Content filename -Tail 10}"

Create the aliases head and tail either in iPython or Jupyter :

  %alias head powershell -command "& {Get-Content %s -Head 10}"

  %alias tail powershell -command "& {Get-Content %s -Tail 10}"

Usage : You should now be able to the commands like this :

 %head filename

to show the first 10 lines of filename.

 %tail filename

to show the last 10 lines of filename.

To persist the aliases across sessions, store them with the %store magic command :

  %store head
  %store tail

When you start a new iPython/Jupyter session you need to restore the stored variables with

  % store  -r

To automatically restore stored variables at startup, add this line to your file :

  c.StoreMagics.autorestore = True

If you do not have a config file yet, create one with the command

   ipython create profile


There are currently no comments

New Comment

In the interest of keeping comments relevant to the subject posted, please note all comments will be moderated. Comments which are out of topic will be deleted.


required (not published)