[Python] How can I write a .log file with all the screen outputs (stdout and stderr) of the...

Discussão em 'Python' iniciado por Stack, Maio 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to write a .log file. I'm executing a Python file where I wrote:

    import sys

    f = open(pathfile_log, 'a')

    sys.stdout=f
    sys.stderr=f


    Later, I executed a .sh script that starts with #!/bin/bash -xe which prints the commands executed and stops the script when there's an error. I need to record, also, the whole output of this execution into the same pathfile_log.

    I'm executing the .sh script with the following code:

    import subprocess

    subprocess.run(str(test_sh_path), stdout=f, stderr=f, check=True)
    # or
    subprocess.run(str(test_sh_path), stdout=sys.stdout, stderr=sys.stderr, check=True)


    Both cases print into the log file because the firsts lines of the Python code which redirects the stdout/stderr to f (.log file descriptor).

    But what I found is that the .sh output messages are in the first part of the .log file. They are not registered chronologically. Do you know how could I solve this?

    I tried to use different file descriptors but it's hard to manage 2 file descriptors. I solved closing the fd and redefining other fd just before the subprocess.run(...) but it's not a clean method.

    Continue reading...

Compartilhe esta Página