Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can redirect to a file if you just want the output in a file for later evaluation.</p> <p>Your already defining the stdout/stderr of the processes your executuing by the stdout=/stderr= methods.</p> <p>In your example code your just redirecting to the scripts current out/err assigments.</p> <pre><code>subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) </code></pre> <p>sys.stdout and sys.stderr are just file-like objects. As the documentation documentation on <a href="http://docs.python.org/library/sys.html?highlight=stdout#sys.stdout" rel="nofollow noreferrer">sys.stdout</a> mentions, "Any object is acceptable as long as it has a write() method that takes a string argument."</p> <pre><code>f = open('cmd_fileoutput.txt', 'w') subprocess.Popen(cmd, shell=True, stdout=f, stderr=f) </code></pre> <p>So you only need to given it a class with a write method in order to re-direct output.</p> <p>If you want both console output and file output may be making a class to manage the output.</p> <p>General redirection:</p> <pre><code># Redirecting stdout and stderr to a file f = open('log.txt', 'w') sys.stdout = f sys.stderr = f </code></pre> <p>Making a redirection class:</p> <pre><code># redirecting to both class OutputManager: def __init__(self, filename, console): self.f = open(filename, 'w') self.con = console def write(self, data): self.con.write(data) self.f.write(data) new_stdout = OutputManager("log.txt", sys.stdout) </code></pre> <p>Interleaving is dependant on buffering, so you may or may not get the output you expect. (You can probably turn off or reduce the buffering used, but I don't remember how at the moment)</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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