我有一個節點服務器,它通過執行外部 python 腳本
child_process execFile()
我用已知數量的引數執行它沒有問題我只是像傳遞它一樣
const { execFile } = require('node:child_process');
const child = execFile('python', [conf.default.python_dir "/script.py", arg1, arg2, arg3, arg4], (error:string, stdout:string, stderr:string) => {
if (error) {
throw error;
}
console.log(stdout);
});
但是我需要傳遞不斷變化的引數數量,因為我不知道我的資料將包含多少個元素。我如何將陣列的所有元素(長度變化)傳遞到這個 python 腳本中(作為引數)。
我無權訪問 python 腳本,但理想情況下,python 腳本能夠讀取無限數量的引數,我只能通過命令列傳遞引數
uj5u.com熱心網友回復:
使用展開操作來展開陣列 ( ...),您可以創建一個接受無限數量引數的函式,并將這些引數展開到execFile
const { execFile } = require('node:child_process');
function execFileWithArgs(...args) {
const child = execFile('python', [conf.default.python_dir "/script.py", ...args], (error:string, stdout:string, stderr:string) => {
if (error) {
throw error;
}
console.log(stdout);
});
}
execFileWithArgs("unlimited","arguments","here",":)")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491805.html
標籤:javascript 节点.js 命令行 论据 子进程
