2

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Yes, I'm storing it in a .py file but how can I run the file from the javascript coz, I don't want to run it by manually opening cmd and all. I want that to be done automatically whenever the script is being written on .py file. I'm a complete beginner, sorry if I'm asking basic questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.