asteroid4u
asteroid4u

Reputation: 79

Redirect output to two files in python

I am writing code where i want to redirect output two different files in python

one for log purpose and another for fabric code creation.

Below is the code which redirect output to two different files:

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__

In Above code creating one for log file Storage_file to store this file for records and Fabric_code file to genarate fabric code.

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.

Instead of this is there any solution where i can redirect print output to two direct files without opening and closing

Upvotes: 1

Views: 2029

Answers (1)

R. Q.
R. Q.

Reputation: 904

You should refactor your code by opening the files once in the beginning, then writing your files and closing the files in the end. From the above example we can do the following:

import sys

# Open files
storage_file = open("Storage-output.txt", 'w')
fabric_file = open("Fabric_code.py", 'w')

# ===================
# Write Block
# ===================
print('###################################Creating storage commands         Variables##########################')
sys.stdout = storage_file
print ('network port show')
print ('Storage Commands are completed')
sys.stdout = sys.__stdout__  

# write fabric code
print"from fabric.api import run"
print"def host_type():"
print"    run('rows 0', shell=False)"
print"    run('networ port show', shell=false)"
sys.stdout = sys.__stdout__
sys.stdout = storage_file
print('###############Genarating aggregate command#############')
print ('storage aggregate create -aggregate aggr_fab -diskcount 6 -raidtype    raid_dp", shell=False')
sys.stdout = sys.__stdout__  
# write fabric code
print"   run('storage aggregate create -aggregate aggr_fab -diskcount 6 -  raidtype raid_dp', shell=False) "
sys.stdout = sys.__stdout__

# closing files
storage_file.close()
fabric_file.close()

Upvotes: 2

Related Questions