Nodejs使用PythonShell运行python脚本

时间:2017-06-02 19:57:13

标签: javascript python node.js mongodb

实际上,我可以运行这个python脚本,当我在终端中运行它时,这个脚本会创建csv文件。但是,当我在nodejs后端运行此文件时,此文件无法创建csv文件。我在哪里犯错误?

这是pythonshell主页:https://github.com/extrabacon/python-shell

var options = {
     mode: 'text',
     scriptPath: './views',
     args: [informations["camera_id"], informations.start_s, informations.start_min, informations.start_h, informations.start_d, informations.start_mon, informations.start_y, informations.end_s, informations.end_min, informations.end_h, informations.end_d, informations.end_mon, informations.end_y]
     };

var pyshell = new PythonShell('historical_inquiry_1.py', options);
     pyshell.on('message', function (message) {
     console.log("script message : ",message);
     });

这里是python脚本,(test是一个数组):

...
with open("test.csv", "w") as text_file:
        text_file.write(test)
...

实际上,我会用它来获取巨大的查询结果。正常方式结果是超时。你能不能给我的错,我们更好的方法来获取巨大的mongodb查询结果。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

检查nodejs工作目录是否是包含python脚本views/historical_inquiry_1.py的目录。我建议你在scriptPath的{​​{1}}中使用绝对路径。