Python 子进程 FileNotFoundError [英] Python subprocess FileNotFoundError
问题描述
我正在尝试关注 这个博客 关于如何从 Python 执行 R 脚本.我让 R 脚本从命令行使用 Rscript 正常工作.
I am trying to follow this blog on how to execute an R script from Python. I have the R script working fine from the command line using Rscript.
这是我的 Python 代码:
Here's my Python code:
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] 系统找不到指定的文件
FileNotFoundError: [WinError 2] The system cannot find the file specified
我已经阅读了很多关于此错误的 SO 帖子,在大多数情况下,它似乎是 尝试调用系统命令,例如 dir
或将参数传递给 check_output
顺序错误,但就我而言,我真的不知道应该出什么问题.
I've read a lot of SO posts on this error and in most cases it seems to be a problem with trying to invoke system commands like dir
or passing arguments to check_output
in the wrong order but in my case I really don't see what should be going wrong.
遵循一些建议我已经尝试为 cmd
构建一个字符串而不是一个列表,然后使用参数 shell = True
将它传递给 check_output
- 当我这样做我得到一个 CalledProcessError:返回非零退出状态 1.
Following some of the advice I've tried building a string for cmd
instead of a list, and then passing it to check_output
using the argument shell = True
- when I do that I get a CalledProcessError: returned non-zero exit status 1.
我假设这段代码(除了添加文件的绝对路径之外与博客上显示的完全一样)现在失败了,因为 check_output
的行为自 2015 年以来发生了变化..
I'm assuming this code, which is exactly as it appeared on the blog other than adding the absolute path to the file, is failing now because the behaviour of check_output
has changed since 2015...
有人可以帮忙吗?
这是堆栈跟踪:
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
推荐答案
检查命令和脚本的路径是否正确
check that you have a right path for command and script
print(os.path.exists(command))
print(os.path.exists(path2script))
请注意,使用反斜杠写入路径可能很危险,因为您可以以不同的方式创建转义序列.您可以使用正斜杠编写 Windows 路径,然后对它们调用 os.path.normpath,将它们转换为安全形式(同样在命令中你只能使用正斜杠,Python 解释并不真正关心.在你的 R 脚本的路径中,这可能是问题)
note that writing path with backslashes may be dangerous as you can create escape sequence that way which will be interpreted in different way. You can write windows paths with forward slashes and then call os.path.normpath on them, turning them into safe form (also in command you can use forward slashes only, Python interpret doesn't really care. In path to your R script that would be probably problem though)
这篇关于Python 子进程 FileNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!