using python at the bash shell?

Simon Forman rogue_pedro at yahoo.com
Tue Aug 8 01:35:55 EDT 2006


skip at pobox.com wrote:
> John> Aside from the normal commands you can use, I was wondering if
>     John> it's possible to use Python from the terminal instead of the
>     John> normal bash commands (e.g. print instead of echo).
>
> Take a look at ipython <http://ipython.scipy.org/>.  It's not precisely what
> you've asked for, but it provides some features that help integrate Python
> with the shell environment.
>
> Skip

FWIW, I second this.  If you want to use python as your shell use
Ipython.  It's just great.  Fernando Pérez has done a Good Thing IMHO.

Be sure to read (or at least skim) the manual:
http://ipython.scipy.org/doc/manual/index.html

Here's an example of calling "man ls" and then getting the results of
"ls -l" into a python list (one list item per line) and then extracting
the size (as a string) of the MANIFEST file...   Enjoy...   (I hope
reformatting doesn't screw this up too badly.)


$ ipython
Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
Type "copyright", "credits" or "license" for more information.

IPython 0.7.1.fix1 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: !man ls
Reformatting ls(1), please wait...
<viewing actual man page here. ~Simon>

In [2]: !!ls -l
Out[2]:
['total 40',
 'drwxr-xr-x 3 sforman sforman 4096 2006-08-02 12:41 docs',
 'drwxr-xr-x 3 sforman sforman 4096 2006-04-27 23:49 logs',
 '-rw-r--r-- 1 sforman sforman  887 2006-04-27 23:49 MANIFEST',
 '-rw-r--r-- 1 sforman sforman  173 2006-05-04 14:04 MANIFEST.in',
 '-rw-r--r-- 1 sforman sforman 3338 2006-06-14 20:19 README',
 '-rw-r--r-- 1 sforman sforman  475 2006-07-16 17:40 setup.py',
 'drwxr-xr-x 4 sforman sforman 4096 2006-08-07 15:04 src',
 'drwxr-xr-x 3 sforman sforman 4096 2006-06-21 20:59 tests',
 '-rw-r--r-- 1 sforman sforman  898 2006-07-09 21:53 TODO',
 'drwxr-xr-x 3 sforman sforman 4096 2006-08-05 17:37 util']

In [3]: data = [n.split() for n in _[1:]]

In [4]: print data[2][4]
887

In [5]:


etc...

HTH,
~Simon




More information about the Python-list mailing list