-1

I want to write a python code to run the job of the following Shell script for me:

for run in {1..100}
do
/home/Example.R
done

The shell script is basically running an R script for 100 times. Since I am new to python can somebody help me to write this code in python?

A.M.
  • 1,757
  • 5
  • 22
  • 41
  • You can call shell script directly from python, look at [this](http://stackoverflow.com/questions/3777301/how-to-call-a-shell-script-from-python-code) thread for more info. – Elias Jun 23 '14 at 18:11
  • @Elias you are right but I want to learn how can I run that R script directly from python without running this Shell script. – A.M. Jun 23 '14 at 18:13

2 Answers2

1

You could use subprocess.call to make an external command:

from subprocess import call

for i in xrange(100):
  call(["/home/Example.R"])
RohitJ
  • 543
  • 4
  • 8
0

You can use python's commands module to execute external commands and capture their output. The module has a function commands.getstatusoutput(cmd), where cmd is the command you are looking to run, as a string.

Something like this could do the trick:

import commands
for x in xrange(100):
  commands.getstatusoutput("/home/Example.R")

Each iteration of the for loop, the commands.getstatusoutput() function will even return a tuple (status, output), where status is the status after executing the program, and output being anything that the command would have written to stdout.

Hope that helps.

jpugliesi
  • 26
  • 4