Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run from ipython with glob style expansion?

Tags:

ipython

glob

In ipython (0.10.2), I'd like to run with bash shell style (or glob.glob) expansion. I'd like to do something like these and have the arguments expand out just as they would if I were in bash. Is this possible?

filenames = !ls *.sbet
run sbet.py $filenames
# fails because filenames is a list

# or the simpler case:
run sbet.py *.sbet
# failes with "*.sbet" being directly passed in without being expanded.

Suggestions? Thanks!

like image 223
Kurt Schwehr Avatar asked Feb 22 '23 08:02

Kurt Schwehr


1 Answers

The return of filenames = !ls *.sbet is a special list, with a few extra attributes:

  • filenames.l - the list
  • filenames.s - a whitespace-separated string
  • filenames.n - a newline-separated string

do filenames? in IPython for more info.

So to pass the args to run, you want filenames.s:

filenames = !ls *.sbet
run sbet.py $filenames.s

If you have a regular list, then you will need to turn it into a string yourself, before passing to run:

filenames = glob.glob('*.sbet')
filenames_str = ' '.join(filenames) # you will need to add quotes if you allow spaces in filenames
run sbet.py $filenames_str

You might make a feature request for automatic expansion of file globs when passed to %run if run foo.py *.sbet is valuable to you.

like image 151
minrk Avatar answered Mar 07 '23 13:03

minrk