Search code examples
ipythonglob

run from ipython with glob style expansion?


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!


Solution

  • 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.