Python子进程FileNotFoundError
我试图继续介绍如何从Python执行R脚本。我使用Rscript在命令行中运行R脚本 以下是我的Python代码:Python子进程FileNotFoundError,python,subprocess,rscript,file-not-found,Python,Subprocess,Rscript,File Not Found,我试图继续介绍如何从Python执行R脚本。我使用Rscript在命令行中运行R脚本 以下是我的Python代码: import subprocess import os command = "C:\Program Files\R\R-3.4.4\bin\Rscript" path2script = os.getcwd() + "\max.R" # gives me the absolute path to the R script args = ["11", "3", "9", "42"]
import subprocess
import os
command = "C:\Program Files\R\R-3.4.4\bin\Rscript"
path2script = os.getcwd() + "\max.R" # gives me the absolute path to the R script
args = ["11", "3", "9", "42"]
cmd = [command, path2script] + args
x = subprocess.check_output(cmd, universal_newlines = True)
这给了我一个错误:
FileNotFoundError:[WinError 2]系统找不到指定的文件
我已经阅读了很多关于这个错误的SO帖子,在大多数情况下,这似乎是像dir
或将参数传递给check\u output
的问题,但在我的情况下,我真的看不出应该出什么问题
接下来,我尝试为cmd
而不是列表构建一个字符串,然后使用参数shell=True
将其传递给check\u output
——当我这样做时,我得到一个调用的进程错误:返回非零退出状态1。
我假设这段代码现在失败了,因为自2015年以来,check\u output
的行为发生了变化,这段代码与博客上显示的代码完全相同,而不是向文件添加绝对路径
有人能帮忙吗
以下是堆栈跟踪:
Traceback (most recent call last):
File "<ipython-input-2-3a0151808726>", line 1, in <module>
runfile('C:/Users/TomWagstaff/Documents/Raising IT/Projects/15 AdWords/Python_R_test/run_max.py', wdir='C:/Users/TomWagstaff/Documents/Raising IT/Projects/15 AdWords/Python_R_test')
File "C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/TomWagstaff/Documents/Raising IT/Projects/15 AdWords/Python_R_test/run_max.py", line 31, in <module>
x = subprocess.check_output(cmd, universal_newlines = True)
File "C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\site-packages\spyder\utils\site\sitecustomize.py", line 210, in __init__
super(SubprocessPopen, self).__init__(*args, **kwargs)
File "C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
runfile('C:/Users/TomWagstaff/Documents/Raising IT/Projects/15 AdWords/Python\u R\u test/run\u max.py',wdir='C:/Users/TomWagstaff/Documents/Raising IT/Projects/15 AdWords/Python\u R\u test')
文件“C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\site packages\spyder\utils\site\sitecustomize.py”,第705行,在runfile中
execfile(文件名、命名空间)
文件“C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\site packages\spyder\utils\site\sitecustomize.py”,第102行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“C:/Users/TomWagstaff/Documents/Raising IT/Projects/15 AdWords/Python\u R\u test/run\u max.py”,第31行,在
x=子流程。检查输出(cmd,universal\u newlines=True)
文件“C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\subprocess.py”,第336行,在check\u输出中
**kwargs)stdout
文件“C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\subprocess.py”,第403行,正在运行
使用Popen(*popenargs,**kwargs)作为流程:
文件“C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\site packages\spyder\utils\site\sitecustomize.py”,第210行,在\uuu init中__
super(子流程,self)。\uuuuu初始化(*args,**kwargs)
文件“C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\subprocess.py”,第709行,在\uuu init中__
恢复信号,启动新会话)
文件“C:\Users\TomWagstaff\Anaconda3\envs\adwords\lib\subprocess.py”,第997行,在执行子进程中
startupinfo)
FileNotFoundError:[WinError 2]系统找不到指定的文件
检查命令和脚本的路径是否正确
print(os.path.exists(command))
print(os.path.exists(path2script))
请注意,带反斜杠的写入路径可能很危险,因为您可以用这种方式创建转义序列,而转义序列将以不同的方式进行解释。您可以使用正向斜杠编写windows路径,然后对其调用os.path.normpath,将其转换为安全形式
(同样在命令中,您只能使用正向斜杠,Python解释并不真正关心。在R脚本的路径中,这可能是个问题)您可以发布错误的堆栈跟踪吗?此方法是否在堆栈错误跟踪中的任何位置调用?“NamedTemporaryFile()”您应该尝试使用正斜杠(
/
)-path2script=os.getcwd()+“/max.R”
就是这样!刚刚发现我的“\bin”在最后一个字符串中变成了一个特殊字符(退格?)。像这样转义反斜杠:“\\bin”在整个命令
字符串中修复了该问题。。。