Note that there are some explanatory texts on larger screens.

plurals
  1. POPython subprocess.check_call - how to direct stdout and stderr to _both_ screen and log file?
    primarykey
    data
    text
    <p>I use Python 2.7. This is what I'm doing currently to launch other programs from Python, redirecting STDOUT and STERR to LOGFILE:</p> <pre><code> try: # Execute command and print content to LOGFILE tempname = os.path.abspath('./text.txt') TEMPFILE = open(tempname, 'wb') print 'Executing: ', command subprocess.check_call(command, shell = True, stdout = TEMPFILE, stderr = TEMPFILE) TEMPFILE.close() LOGFILE.write(open(tempname, 'rU').read()) LOGFILE.close() os.remove(tempname) except Exception as errmsg: # If fails then print errors to LOGFILE TEMPFILE.close() LOGFILE.write(open(tempname, 'rU').read()) os.remove(tempname) print messages.crit_error_bad_command % command, '\n', str(errmsg) print &gt;&gt; LOGFILE, messages.crit_error_bad_command % command, '\n', str(errmsg) LOGFILE.close() </code></pre> <p>First of all, I wasn't sure my above script was the best solution. I had to use a temp file because I want to capture the log even in the case the subprocess.check_call call fails. If you have ideas how to improve the above script I'd appreciate it.</p> <p>Moreover, I would like to change this so that STDOUT and STDERR should go to the screen as normal AND also to the log file. How do I do this? Note that if I don't specify STDOUT, I'll see things like "There is an error, do you wish to continue [y/n]?" on the screen and then I can react to it. Right now since everything goes to the log, I do not see anything on the screen. The answer to my question here should help resolve this case.</p> <p>Thanks. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload