0

As mentioned above,I would like to capture everything that is printed on the screen by Python to a file.

I've found these great answers, but they all use more or less the same approach and don't capture everything. Specifically I try to capture the output of YouTubeDL, the Python package, not the command line tool, but it's not saved if I just capture sys.stdout.

P.S.: I'm using Windows.

csabinho
  • 1,579
  • 1
  • 18
  • 28
  • 1
    Do you know how it's generating its output? If it's writing to `sys.stderr`, you can capture that the same way. But if it's writing to `/dev/tty` you'll need to use a pseudo-tty. – Barmar Mar 27 '22 at 09:00
  • @Barmar I'm capturing `sys.stderr` as well. It's just used for exceptions. – csabinho Mar 27 '22 at 09:04
  • On Unix there's the `script` command that captures all the input and output of a terminal application. Maybe there's something analogous on Windows. – Barmar Mar 27 '22 at 09:05
  • @Barmar I could run it externally and capture it via `>>` and `2>>`, but I want to solve it in Python. – csabinho Mar 27 '22 at 09:34
  • If `>>` works externally, then reassigning `sys.stdout` should work. – Barmar Mar 27 '22 at 09:36
  • @Barmar I agree. It would be logical, but sadly it's not like that. – csabinho Mar 27 '22 at 10:45
  • Is YouTubeDL open source? Check the source code to see what it's doing that gets around this redirection. – Barmar Mar 27 '22 at 15:13
  • @Barmar The point is: I'm looking for a general solution that covers everything. `YouTubeDL` was just the reason and an example. But yes, it's open source! :) – csabinho Mar 27 '22 at 18:34
  • For practically everything, redirecting `sys.stdout` should work. There's something very weird about YouTubeDL. Without knowing what it is, it's hard to come up with a general solution that also handles it. – Barmar Mar 27 '22 at 19:08

0 Answers0