JavaScript Spawn:如何在Spawn python脚本中将变量传递给FLAG

问题描述 投票:2回答:1

我有一个python脚本有两个FLAGs --server--image

现在,在JavaScript中,我只能使用spawn将固定值分配给FLAGS。例如:(这确实产生了输出)

var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=./testImage/DSC00917.JPG']);

pyProg.stdout.on('data', function (data) { console.log('This is result ' + data.toString());});

但是,我想分配一个字符串变量并将字符串传递给FLAG。例如:(这是错误的,它不会产生任何输出)

var imagePath = './testImage/DSC00917.JPG'

var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=imagePath']);

pyProg.stdout.on('data', function (data) { console.log('This is result ' + data.toString());});

我应该如何使它工作?先感谢您!

javascript python node.js spawn
1个回答
0
投票

您可以像在JavaScript中的任何其他位置一样使用字符串连接。如果您希望console.log打印变量,您可以这样做:

console.log('image path is ' + imagePath);

或者如果您使用的是ES6字符串插值:

console.log(`image path is ${imagePath}`);

这同样适用于您的代码示例:

var imagePath = './testImage/DSC00917.JPG'
var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=' + imagePath]);
© www.soinside.com 2019 - 2024. All rights reserved.