2017-04-03 7 views
0

Ich schreibe Code, wo ich möchte Ausgabe zwei verschiedene Dateien in Pythonin Python

einen für log Zweck und eine andere für Stoff-Code Erstellung umleiten.

Unten ist der Code, die Ausgabe auf zwei verschiedene Dateien umleiten:

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__ 

oben Code ein für Protokolldatei Storage_file Erstellung dieser Datei zu speichern, für Aufzeichnungen und Fabric_code Datei Stoff Code genarate.

Mein Code generiert 1000 von Befehlen, die ich nicht mehrmals im Python-Code für zwei verschiedene Dateien öffnen und schließen möchte.

Statt dessen ist es eine Lösung, wo ich die Druckausgabe auf zwei direkte Dateien umleiten können ohne Öffnen und Schließen

+0

asteroid4u: Läuft die unten genannte Lösung nicht für Sie? –

Antwort

0

Sie Ihren Code durch das Öffnen der Dateien einmal am Anfang Refactoring sollte, dann Ihre Dateien zu schreiben und Schließen der Dateien am Ende. Aus dem obigen Beispiel können wir Folgendes tun:

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()