Graindor
Graindor

Reputation: 59

Running python script on cmd(Windows) through java script

I have an application where the output is written into a file (.py) by using javascript.

I'm using this application to write python script into the file. Now I want to run the python script automatically on cmd(Windows) right after the output was written.

Is there a way to do so ? Is it possible without using "NodeJS"

So apparently everything happens with a single click on the application. Thanks.

Upvotes: 2

Views: 1032

Answers (2)

user10251509
user10251509

Reputation:

Why not storing your script in a script.py file? Why do you use .txt at all? With CMD and Python installed you should easily run .py scripts with a command line "python path/to/script.py", shouldn't you?

Edit: For checking out how to execute python on Node JS just use Google! Google is your friend! "execute python with node js" threw me this article: How to call python script from NodeJs

Upvotes: 1

Alien128
Alien128

Reputation: 313

Node js provides the child process module, which you can use to basically spawn a child process from your js application.

since you have not shared any source code so i am not sure what your specific use case is but a basic way of spawning python script would be like this.

import { spawn } from 'child_process';
let scriptPath = './script.py'    // path to your python script
var script = spawn('python', ['-u', scriptPath, arg1, arg2]);  // arg1,arg2 can be any command line arguments required by your script or if not needed you can skip them.
script.stdout.on('data', data => {
    console.log('Data: ', data.toString());
    // implement your functionality here
});

you can similary bind on close and error events to your script and implement the functionality accordingly.

Upvotes: 1

Related Questions