0

Is there an iPython equivalent to executing the Unix sh command? I'd like to execute multiple iterations of a program via a shell file that has a set of lines such as:

%run python_file.py data_file1
%run python_file.py data_file2
.
.
.
%run python_file.py data_fileN

The python_file.py has sys module argv calls to allow command line inputs.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318

1 Answers1

1

in the directory where you the python_file.py is located you create a python file called run_python_file.py (or somthing like that), it should contain

from subprocess import call
from sys import argv 

if len(argv) <= 1:
    print "Usage..."

for i in range(0,int(argv[1])): #n has to be defined somewhere!
    call(['python','python_file.py','data_file'+str(i)])

In your iPython shell you navigate to the folder where python_file.py is located and then call

%run run_python_file.py 5

if you want to run it with datafiles from 0 to 4

wastl
  • 2,643
  • 14
  • 27
  • Thanks so much for the quick response! I really like the idea to use the "call" method from subprocess, but when I run the code I'm getting a "WindowsError: [Error 2] The system cannot find the file specified". Any ideas for a work around? – user3671396 May 24 '14 at 15:26
  • I think this might interest you: http://stackoverflow.com/questions/9531683/problems-using-subprocess-call-in-python-2-7-2-on-windows – wastl May 24 '14 at 15:56
  • Nice, thanks! When I add the parameter "shell=True" it solves the WindowsError issue, but I'm not getting the call output to execute in the command line. I tried added 'cmd' as recommended in that link, but it's just crashing Canopy. – user3671396 May 25 '14 at 09:41
  • I just search for completely different stuff to make a linux command run in windows from within geany editor. Answer is also completely irrelevant. But finally this post gave me an idea for a different approach which solved my problem. Hurray. Upvote for your answer. – mythicalcoder Jul 24 '15 at 12:50