Redirect output of python script in new cmd into a text file

Viewed 34

I would run a child process from NodeJS start cmd.exe python test.py arg1 arg2 >> output.txt all on windows environment.

However, no output is shown in the text file. But when I remove the start cmd.exe and simply do python test.py arg1 arg2 >> output.txt then I can see the output.

How can I elegantly redirect output in a new cmd window running a python script into some text file?

In fact, the main reason why I start the python in a new cmd is so I can real-time see the console output, I do not know a very good way to simultaneously log console output into a text file, print it into cmd live, and send to the server to display to end users all at once. It seems I can only do one or the other. Also, I have searched about printing to console + logging to a text file, but most of the answers are about logging and then printing to console the entire output of the whole program lifetime. Yet I want to do it print and log bit by bit rather than at once in the end.

Extra info: Ultimately I want to monitor a python script running and log any possible errors and progression stages into a text file. Currently, I use HTTP messages to push print statements into a NodeJS server, however, it does not push error messages if the python script dies. I want to let the user know these error messages too. In fact, the python script and NodeJS server are on the same machine, it would be even more efficient I believe to have the python script redirect the print statements into the server javascript but I do not know how to do it (maybe with some stdout) and so I resort to using HTTP messages.

0 Answers
Related