bigTree
bigTree

Reputation: 2183

Error when calling a file from python

I want to call an R program from a python script.

I wrote the following:

os.system("cat " + variableName + " | code.R")

It returns the error: sh: 1 : code.R: not found cat: write error: Broken pipe

Yet, I am sure of the name of the R file.

Why is it not working?

Upvotes: 0

Views: 74

Answers (2)

Raydel Miranda
Raydel Miranda

Reputation: 14360

So, if code.R is a script that has to be interpreted you must build the pipe to the interpreter not to the script. You receive a Broken PIPE error because code.R by it self don't know how to handle command line arguments.

On the other hand if what you want is store the variable value inside code.R you have to change | by >>.

os.system("cat " + variablename + ">> code.R")

EDIT: Since it's working from terminal, try this:

import subprocess
input = open(variableName, "r")
result = suprocess.call(["code.R"], stdin=input)    # result is the return code for the command being called.

see subprocess.call for more details.

Upvotes: 1

Russell Borogove
Russell Borogove

Reputation: 19057

Is code.R in the current working directory? Is it executable? Can you run cat xxx | code.R from the shell and have it work properly, instead of running your python program?

Upvotes: 1

Related Questions