0

I have

subprocess.call(['errcalc', os.path.join(current_out_dir,
                'drclog')], stdout=error_summary)
subprocess.call(['errcalc', os.path.join(current_out_dir,
                'drclog')], stdout=full_error_summary)

Instead of running the command twice, can I have subprocess.call() output to multiple stdout? Perhaps subprocess.call(['errcalc', os.path.join(current_out_dir, 'drclog')], stdout=[error_summary, full_error_summary]) or something?

Solution

subprocess.call(['errcalc', os.path.join(current_out_dir,
                'drclog')], stdout=logfile)
for line in logfile:
    error_summary.write(line)
    full_error_summary.write(line)
Community
  • 1
  • 1
bmikolaj
  • 485
  • 1
  • 5
  • 16
  • 1
    Related, possibly duplicate: http://stackoverflow.com/questions/2996887/how-to-replicate-tee-behavior-in-python-when-using-subprocess – Daniel Pryden Oct 05 '14 at 21:06
  • @DanielPryden: the question that you've linked is more complex (it requires to capture and display immediately both stdout/stderr). The solution is much simpler if you need only one stream. There is also no real-time requirement here. Compare [these](http://stackoverflow.com/a/4985080/4279) [solutions that capture both stdout/stderr separately in real time](http://stackoverflow.com/a/25960956/4279) and [this solution that merges stdout/stderr](http://stackoverflow.com/a/25968448/4279). btw, the first Python code example in the last link also answers this question: single source, multiple sinks – jfs Oct 06 '14 at 12:52
  • 1
    @p014k: Have you tried replacing [`logfile.write(line)`](http://stackoverflow.com/a/25968448/4279) with `error_summary.write(line); full_error_summary.write(line)`? – jfs Oct 06 '14 at 13:05
  • I could write to a third file logfile and then have `for line in logfile; error_summary.write(line); full_error_summary.write(line)`. Thanks. – bmikolaj Oct 06 '14 at 13:51
  • You should post your solution as an answer and not as part of the question. You can always self-accept your own answer. – b4hand Jun 17 '15 at 00:22

2 Answers2

2

A process has a single standard output device. You cannot attach more than one device to the standard output.

What you could do is to pick one standard output device and attach that. Once the process is complete, copy the output to the other device. Alternatively, create a device that multiplexes the output. Create a device that has a reference to the two devices that you ultimately want to receive output. When your attached output device receives output, it pass it on to both of the other devices.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

You could pipe the output to the tee command which will split the output into a file and then another stdout.

b4hand
  • 9,550
  • 4
  • 44
  • 49