Skip to content

Instantly share code, notes, and snippets.

@shubham-sharmas
Created October 30, 2023 15:22
Show Gist options
  • Save shubham-sharmas/ab2ea026636ad3e46492f791afdf59c4 to your computer and use it in GitHub Desktop.
Save shubham-sharmas/ab2ea026636ad3e46492f791afdf59c4 to your computer and use it in GitHub Desktop.
Node.js child_process.execFile() method example to add two numbers using python script
import sys
def add_numbers(number1, number2):
try:
result = float(number1) + float(number2)
print(result)
except ValueError:
print("Invalid input. Please provide valid numbers!!!")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("add_numbers.py <number1> <number2>")
sys.exit(1)
number1 = sys.argv[1]
number2 = sys.argv[2]
add_numbers(number1, number2)
const { execFile } = require('child_process');
const path = require('path');
const pythonScriptPath = path.join(__dirname, 'add_numbers.py');
const num1 = 10;
const num2 = 17;
execFile('python', [pythonScriptPath, num1, num2], (error, stdout, stderr) => {
if (error) {
console.error(`Error executing Python script: ${error}`);
return;
}
console.log('Python Script: Executed successfully!');
console.log('Result:', stdout);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment