1 year ago

#376573

test-img

Sajjad Ahmed Shaaz

NodeJS child_process send input

I have a python script as follows :

print ("hello world ")
x = input("Enter a number: ")
print(x)

Now I want to programitically run the same using the below NodeJS code:

var spawn = require("child_process").spawn;

child = spawn("python", ["./test.py"]);


child.stdout.on("data", (data) => {
  console.log(`${data}`);
  child.stdin.write("7\n");
});

But the output isn't as expected and is like below:

hello world 
Enter a number: 
7

Is there any way I can send input to the node child process once the script awaiting for input?

javascript

python

node.js

io

child-process

0 Answers

Your Answer

Accepted video resources