Python使用子进程来管道R脚本,生成Winerror 2

2024-10-01 22:40:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图使用subprocess通过Python运行R脚本,但不幸的是,我不断遇到以下错误:

WindowsError: [Error 2] The system cannot find the file specified

这是非常令人沮丧的,因为我已经检查了所有的路径多次(没有C:/,移动到不同的目录,改变了我试图运行的R脚本,等等)。我还检查了CompSec环境变量,它也是正确的(通过Windows系统,尽管我从未在IDE Spyder中直接检查过它)。我试过在Python2.7和Python3.5中使用它,但两个版本都不起作用。你知道吗

代码如下:

import subprocess

def sort_ByInputColumn(inputPath, inputFileTermination, sortColumn, outputPath, outputFileTermination):
scriptPath = "C:/Users/Kyle/Documents/Rscript_SortByInputColumn.R"
subprocess.call(["Rscript",  scriptPath, inputPath, inputFileTermination, sortColumn, outputPath, outputFileTermination])

fileName = 'Alabama'
outputPath = "C:/Users/Kyle/Documents/HillData/Data/Output/Module2/"
sortColumn = str(16)
inputTermination = fileName + 'Module2NN_WorkCounty_Work.csv'
outputFileTermination = fileName + 'Module2NN_SortedWorkCounty.csv'
sort_ByInputColumn(outputPath, inputTermination, sortColumn, outputPath, outputFileTermination)

无论我尝试运行什么代码(甚至公然复制粘贴this tutorial以使其工作),我都会遇到这个错误,这一事实让我觉得有更深层次的(或非常明显的)事情正在发生,而我却看不到。你知道吗

如果您对此事有任何反馈,我们将不胜感激。你知道吗


Tags: 代码脚本错误filenamesortuserssubprocesskyle
1条回答
网友
1楼 · 发布于 2024-10-01 22:40:12

为了解决这个最烦人和可怕的问题,我将R重新安装到一个没有空格的目录中,并用它的完整路径调用了Rscript(也就是从C:/Program Files/R/R-3.3.1/bin/Rscript.exe"调用"C:/R/R-3.3.1/bin/Rscript.exe",因为Program Files有一个空格,我想这会杀死subprocess,因为它是基于命令行的)。这次成功了。你知道吗

请参阅this similar question以获取有关我从何处获得灵感的提示。你知道吗

相关问题 更多 >

    热门问题