contextlib

Catch stdout/stderr from 3rd party modules:

from contextlib import redirect_stdout, redirect_stderr
from io import StringIO
 
with redirect_stdout(StringIO()) as tmp_out:
    with redirect_stderr(StringIO()) as tmp_err:
        esptool.main(command)
out = tmp_out.getvalue()
err = tmp_err.getvalue()