1

How do I redirect the output of python version into a text file in linux?

I tried the following:

python --version > old.txt

The text file is getting created but it is empty. Simple question, but beginner here.

SG213
  • 65
  • 1
  • 7
  • Possible duplicate of [Redirect stdout to a file in Python?](https://stackoverflow.com/q/4675728/608639), [How to redirect stderr in Python?](https://stackoverflow.com/q/1956142/608639), [Temporarily Redirect stdout/stderr](https://stackoverflow.com/q/6796492/608639), [Redirect stderr and stdout in Bash](https://stackoverflow.com/q/637827/608639), etc – jww May 18 '19 at 02:22

1 Answers1

4

The Python version is output to stderr rather than stdout which is what your command is writing to the file. In order to write the contents of stderr you will want to write:
python --version 2> old.txt

0x777C
  • 993
  • 7
  • 21
  • Any reason why it gets output to the STDERR stream? – SG213 May 18 '19 at 00:45
  • Not particularly, some programs just prefer to use stderr to print meta-information (as well as the usual warnings and error messages). – 0x777C May 18 '19 at 00:48