title: Redirect output to two files in python
body: <p>I am writing code where i want to redirect output two different files in python</p>
  
  <p>one for log purpose and another for fabric code creation.</p>
  
  <p>Below is the code which redirect output to two different files:</p>
  
  <pre><code>import sys
  print('###################################Creating storage commands         Variables##########################')
  storage_file = open("Storage-output.txt", 'w')
  sys.stdout = storage_file
  print ('network port show')
  print ('Storage Commands are completed')
  storage_file.close()
  sys.stdout = sys.__stdout__  
  
  # write fabric code
  fabric_file = open("Fabric_code.py", 'w')
  print"from fabric.api import run"
  print"def host_type():"
  print"    run('rows 0', shell=False)"
  print"    run('networ port show', shell=false)"
  fabric_file.close()
  sys.stdout = sys.__stdout__
  storage_file = open("Storage-output.txt", 'a')
  sys.stdout = storage_file
  print('###############Genarating aggregate command#############')
  print ('storage aggregate create -aggregate aggr_fab -diskcount 6 -raidtype    raid_dp", shell=False')
  storage_file.close()
  sys.stdout = sys.__stdout__  
  # write fabric code
  fabric_file = open("Fabric_code.py", 'a')
  print"   run('storage aggregate create -aggregate aggr_fab -diskcount 6 -  raidtype raid_dp', shell=False) "
  fabric_file.close()
  sys.stdout = sys.__stdout__
  </code></pre>
  
  <p>In Above code creating one for log file Storage_file to store this file for records and  Fabric_code file to genarate fabric code.</p>
  
  <p>My code generates 1000's of commands I do not want to open and close multiple times for two different files again and again in the python code.</p>
  
  <p>Instead of this is there any solution where i can redirect print output to two direct files without opening and closing </p>