I want to redirect all output (stdout and stderr) of console to the text file. I make the following steps:

  1. Open cmd.exe
  2. Start command:

    "python.exe" > "file.txt"

After that, I'm waiting for the python's output in the file, but it's still in console. What I'm doing wrong?

  • 1
    You've forgotten to put the name of the Python program, something like: python.exe program.py > file.txt – Dominique Dec 9 '16 at 13:40
  • 4
    You need > file.txt 2>&1 to get stderr as well as stdout – DavidPostill Dec 9 '16 at 14:46
  • @Dominique, No. I haven't forgot. I want to work with python in interactive mode and send commands through console instead of writing script. – neo Dec 12 '16 at 6:54
  • 1
    Have a look at this thread - there is much there, as it's a bit of a tricky thing. And, the asker added comments to summarize what was useful for them. See here: stackoverflow.com/questions/947810/… – qxotk Oct 2 '17 at 6:53
  • @condiosluzverde Great find, this may be a duplicate question... Unless... OP, is there a reason you are trying to do it via CMD vs this native Python method? – primohacker Jan 24 '19 at 1:27

Instead of CMD.exe, you could use the Anaconda prompt.

In the Anaconda prompt, the code you described above works with the Python output going to a file and still being able to interact a the Python prompt.

Anaconda prompt

Tha Anaconda prompt does have a very similar look and behaviour as the CMD prompt. This thread has info on the difference between them.

  • Just tried it in Python 3.6.6 and the behaviour was the same on Windows 10 CMD. What version of Python and Windows are you using? – Fernando Eblagon Jan 30 '19 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.