[IPython-user] capture shell output with '!!'

Fernando Perez Fernando.Perez at colorado.edu
Wed Aug 31 13:04:52 CDT 2005


Rakotomandimby Mihamina wrote:
> Hi,
> I saw in the documentation, a while ago, some tips to capture the output
> of a shell command to a variable (a list)
> 
> I lost it (the location), and now i'm obliged to use %sc.
> 
> My needs:
> - I got some mp3/ogg songs from a friend
> - The directories and file names contain spaces
> - I need to rename thoses files and directories to spaceless names,
> replacing spaces with underscore.
> - I use the 'find' of the system and it returns a list of paths.
> (BTW: is there any internal 'find' into python or ipython?)

No.

> - I want to process each element of the list, but I need to store the
> list in a variable before, and that is I lost the documentation on.

> With '!!', I get error syntaxes whatever I try, and I dont find the
> documentation.... :-(

If you post your syntax errors, it may make it easier for us to help.  But 
here are some ways to accomplish what you want (besides Ryan's glob idea, 
which is valid but more verbose):

- As Ville said,

In [3]: !!ls p*.py
Out[3]: ['pplot2.py', 'pplot.py', 'pprint_bug.py', 'pylab_events.py', 
'pylabug.py']

In [4]: myfiles=_

- You can also use the fact that _NN holds output NN indefinitely:

In [5]: for f in _3:
    ...:     if len(f)>9:
    ...:         !wc -l $f
    ...:
5 pprint_bug.py
30 pylab_events.py
2 pylabug.py


- And finally, in pysh mode you have the additional $/$$ support:

planck[~/test]|1> $$a=ls p*.py
planck[~/test]|2> a
               <2> ['pplot2.py', 'pplot.py', 'pprint_bug.py', 
'pylab_events.py', 'pylabug.py']

I hope this helps,

f




More information about the IPython-user mailing list