0

The following line of code writes every printed output in python into the log file:

C:...>python myscript.py > myoutput.log

Unfortunately, this does not print on terminal.

How can I fix this in a simple way?

I am using Windows 10. Would it be fixed if I change to Linux based system like Ubuntu?

le4m
  • 558
  • 7
  • 18
  • @Calculuswhiz I am currently using Win10 in my home. But I have ubuntu in my lab. So any solution viable in ubuntu terminal will be also very helpful for me! – le4m Apr 13 '21 at 15:52
  • Does this answer your question? [Displaying Windows command prompt output and redirecting it to a file](https://stackoverflow.com/questions/796476/displaying-windows-command-prompt-output-and-redirecting-it-to-a-file) – IMSoP Apr 13 '21 at 15:53
  • 3
    On a "unixoid system" (e.g. your Ubuntu) there's a `tee` command for that: `python myscript.py | tee myoutput.log` – L3viathan Apr 13 '21 at 15:54
  • I will check right away on Ubuntu. Thank you very much guys! – le4m Apr 13 '21 at 15:56
  • @L3viathan Firstly, I sincerely aprreciate your reply, thank you very much. I just tried your command on both powershell and ubuntu terminal and it works fine. I have one concern though. The printing on the terminal screen is not online. Is this not really easily fixable? – le4m Apr 13 '21 at 16:06
  • 1
    @jachilles I don't understand the term "online". It's not line-buffered? You can use the command `unbuffer` to fix that, see e.g. [this Q&A](https://stackoverflow.com/q/11337041/1016216). – L3viathan Apr 13 '21 at 17:06

2 Answers2

0

See Tee-Object Example 1 in PowerShell. help Tee-Object -Full

& python myscript.py | Tee-Object -FilePath '.\myoutput.log'

lit
  • 14,456
  • 10
  • 65
  • 119
0

I got what I exactly wanted according to the comment helps and other stack resources.

$ python -u myscript.py | tee myoutput.log

Without -u, the printing on the terminal screen is not real time.

For window users, simply go to powershell rather than cmd.

le4m
  • 558
  • 7
  • 18