1
Node.js에서 파이썬에 인수를 전달하려고 시도하고 있습니다 (child_process
스폰). 또한 Node.js 배열에 지정된 인수 중 하나를 사용하여 특정 Python 함수를 호출하려고합니다. test()
파이썬 함수를 호출한다 명령 줄 인수를 사용하여 파일에서 파이썬 함수 호출
test.js
'use strict';
const path = require('path');
const spawn = require('child_process').spawn;
const exec = (file, fnCall, argv1, argv2) => {
const py = spawn('python', [path.join(__dirname, file), fnCall, argv1, argv2]);
py.stdout.on('data', (chunk) => {
const textChunk = chunk.toString('utf8'); // buffer to string
const array = textChunk.split(', ');
console.log(array);
});
};
exec('lib/test.py', 'test', 'argument1', 'argument2'.length - 2); // => [ 'argument1', '7' ]
exec('lib/test.py', 'test', 'arg3', 'arg4'.length - 2); // => [ 'arg3', '2' ]
번째 인수 여기서 test
lib/test.py
는 :
import sys
def test():
first_arg = sys.argv[2]
second_arg = sys.argv[3]
data = first_arg + ", " + second_arg
print(data, end="")
sys.stdout.flush()
내가 명령 줄에서 어떤 Node.js를하지 않고 파이썬 파일을 실행하려고하면 실행은 다음과 같습니다
$ python lib/test.py test arg3 2
test
, arg3
및 2
은 명령 줄 인수 일 뿐이며 test
은 test()
함수를 호출해야합니다.이 함수는 arg3
, 에 대한 인수는 2
입니다.
감사합니다! 완벽하게 작동합니다! – Lanti
@ 란티 아무 문제 없어, 기꺼이 도와 드리겠습니다. – Avantol13