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) 
5
  • 1
    Related, possibly duplicate: stackoverflow.com/questions/2996887/… Commented Oct 5, 2014 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 solutions that capture both stdout/stderr separately in real time and this solution that merges stdout/stderr. btw, the first Python code example in the last link also answers this question: single source, multiple sinks Commented Oct 6, 2014 at 12:52
  • 1
    @p014k: Have you tried replacing logfile.write(line) with error_summary.write(line); full_error_summary.write(line)? Commented Oct 6, 2014 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. Commented Oct 6, 2014 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. Commented Jun 17, 2015 at 0:22

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.