Created
October 30, 2023 15:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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