从python调用文件时出错

时间:2014-03-11 17:42:03

标签: python

我想从python脚本中调用R程序。

我写了以下内容:

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

它返回错误:sh:1:code.R:not found cat:写入错误:管道损坏

然而,我确定R档案的名称。

为什么不起作用?

2 个答案:

答案 0 :(得分:1)

当前工作目录中有code.R吗?它可执行吗?你可以从shell运行cat xxx | code.R并让它正常工作,而不是运行你的python程序吗?

答案 1 :(得分:1)

因此,如果code.R是必须解释的脚本,则必须为解释器而不是脚本构建管道。您收到Broken PIPE错误,因为它自己code.R不知道如何处理命令行参数。

另一方面,如果您想要的是将variable值存储在code.R中,则必须按|更改>>

os.system(“cat”+ variablename +“>> code.R”)

编辑:由于它在终端上运行,请尝试以下方法:

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

有关详细信息,请参阅subprocess.call

相关问题