How to Access Unix Shell

Fernando Perez fperez528 at yahoo.com
Tue Jun 15 08:28:45 CEST 2004


Neale wrote:

> In a python program, I want to do a Unix directory list and then
> append selected files to each other which, as you know, is just  "cat
> filename2 >> filename1." How do I escape into the shell and then
> return, bringing with me a  directory list?
> 
> Where can I find some examples of mixing shell commands and Python?

[~]> ipython
Python 2.2.3 (#1, Oct 15 2003, 23:33:35)
Type "copyright", "credits" or "license" for more information.

IPython 0.6.1.cvs -- 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]: cd test
/usr/local/home/fperez/test

In [2]: ls
argv.py*  die.py  div.py     err.pyc   image2.eps  ipython.log   strings.py 
t.py
bar.py    div.c   error.py*  exit.py*  image.eps   ramptest.py*  times.sh*
bar.pyc   div.f   err.py     foo.py    image.ps    scopes.py*    tmp/

In [3]: cp t.py tmp/
cp: overwrite `tmp/t.py'? y

In [4]: cd tmp
/usr/local/home/fperez/test/tmp

In [5]: ll
total 8
-rw-r--r--  1 fperez wavelet 91 Jun 15 00:19 strings.py
-rw-r--r--  1 fperez wavelet 43 Jun 15 00:27 t.py

In [6]: cat strings.py >> t.py

In [7]: ll
total 8
-rw-r--r--  1 fperez wavelet  91 Jun 15 00:19 strings.py
-rw-r--r--  1 fperez wavelet 134 Jun 15 00:27 t.py

In [8]: !!ls
Out[8]: ['strings.py', 't.py']

In [9]: for fname in _8:
   ...:     print 'filename:',fname
   ...:
filename: strings.py
filename: t.py


Hope this helps,

f



More information about the Python-list mailing list