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

2024-05-13 23:55:29 发布

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

我有一个python脚本,它有两个标志--server--image。你知道吗

目前,在JavaScript中,我只能使用spawn将固定值赋给标志。例如:(这会产生输出)

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());});

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

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());});

我该怎么做?提前谢谢!你知道吗


Tags: pyimageclientdataservervar标志stdout
1条回答
网友
1楼 · 发布于 2024-05-13 23:55:29

您可以像在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]);

相关问题 更多 >