Search code examples
pythonpython-3.xcommand-prompttext-to-speech

How can I Save a Python Script output to a file when using Command Prompt?


I am using a speech-to-text example from Microsoft example here.

After following the steps, we can run the example with this command (in command prompt)

python3 main.py 

And, in the command prompt all the results are showed. Great!

However, I want to save all the results into a file (could be JSON, txt, csv...). I saw on Stackoverflow some other persons with the same issue here (however this is not really working in my example), and I knew from a previous project that I could save results to a JSON file (with Scrapy), with a command like this:

scrapy crawl Script -o name_json_file.json

Therefore my question: Can I - directly save - all the results (that the Command Prompt shows) to a file when using the command prompt? And if not, what is the best (alternative) way to save the outputs in a file?

Many thanks!


Solution

  • What you are looking for is

    YOUR-COMMAND > c:\PATH\TO\FOLDER\OUTPUT.txt
    

    so in your case its

    python3 main.py > c:\PATH\TO\FOLDER\OUTPUT.txt
    

    If you want to save and also view the output you can do

    python3 main.py > c:\PATH\TO\FOLDER\OUTPUT.txt | type