尝试修复此错误但无法提供任何帮助,感谢新的bie python也在其他问题中查找但无法解决此问题代码段单独工作但整体似乎无法正常工作不知道出了什么问题 的错误
**Traceback (most recent call last):
File "view_log_info.sh", line 139, in <module>
print network_adapter
ValueError: I/O operation on closed file**
#!usr/bin/env/ python
import sys, re, os, getpass
#function to find the packetloss data in pcoip server files
def function_pcoip_packetloss(filename):
lineContains = re.compile('.*Loss=.*') #look for "Loss=" in the file
for line in filename:
if lineContains.match(line): #check if line matches "Loss="
print 'The file has: ' #prints if "Loss=" is found
print line
return 0;
#function to find the decrease in pcoip_server files
def function_pcoip_decrease(filename):
lineContainsDecrease = re.compile('.*Decrease.*')
for line in filename:
if lineContainsDecrease.match(line): #check if line matches "Decrease"
print 'The file has: ' #prints if "Decrease is found"
print line
return 0;
#function to find the disconnect codes in pcoip_server_files
def function_disconnect_codes(filename):
lineContains_disconnectCode = re.compile(r'(?=.*disconnect cause)')
lineContains_Code = re.compile(r'cause\s*\(.*?(\d+)\)')
for line in filename:
if lineContains_disconnectCode.match(line):
print 'The file has: '
print line
code = lineContains_Code.search(line).group(1)
print 'The code is:', code
if code == '000':
print 'PCoIP session terminated due to lost network.'
elif code == '100':
print 'PCoIP connection not established due to failure of PCoIP server initialization. This should not occur. Please contact Teradici support.'
elif code == '101':
print 'PCoIP connection not established due to failure of PCoIP server initialization. This should not occur in normal operation. Note: If the Welcome Screen is enable in the View Administrator a ConnectionTicketTimeout will trigger a disconnect after 15 min with this code'
elif code == '102':
print 'PCoIP session terminated due to VMware View Connection Server (broker) maximum session duration (Connection Server setting) exceeded.'
elif code == '103':
print 'PCoIP session terminated due to the VDI user selecting Logoff or Restart from Windows in the VDI session'
elif code == '104':
print 'PCoIP session terminated due to admin manually disconnected the session from the Administrative Web Interface.'
elif code == '105':
print 'PCoIP session terminated due to login from an alternate location'
print 'OR Pre-connection warmup initialization of PCoIP Server. This is not for actual connection. If the size of the log is under 15 Kbytes, this is a pre-connection warmup startup/shutdown sequence.'
elif code == '200':
print 'PCoIP session terminated due to the user right-clicking the host driver icon in the tray and then selecting Menu > Disconnect. Applicable only to clients connecting to a hard host and not VDI.'
elif code == '201':
print 'PCoIP connection not established due to incompatible host driver version (not used for VDI).'
elif code == '300':
print 'PCoIP session terminated due to the user closing the View Client window or due to the user ending the View application task inside the Windows Task Manager'
elif code == '301':
print 'PCoIP session terminated due to the user clicking the zero clients Disconnect button. Not applicable for soft clients.'
elif code == '302':
print 'PCoIP session terminated due to the user clicking the Disconnect button in the client Administrative Web Interface. Not applicable for soft clients.'
elif code == '303':
print 'The VMware View Connection Server (broker) requested the session to end.'
elif code == '304':
print 'PCoIP session terminated due to Power Sleep disconnect (not used for VDI).'
elif code == '305':
print 'PCoIP session terminated due to user pulling out the smart card used for user authentication.'
elif code == '306':
print 'PCoIP session terminated due to user taking action specified by OneSign to be a disconnect command (for example, double tapping the card).'
elif code == '400':
print 'Zero client and View 4.5 and earlier: PCoIP session terminated due to network issues on TCP/SSL connection (keepalive ping timeout).'
elif code == '401':
print 'PCoIP connection not established due to networking issues or failure to open drivers, such as video, audio, or USB.'
elif code == '402':
print 'PCoIP connection not established due to networking issues.'
elif code == '403':
print 'PCoIP session terminated due to various reasons. For example, network lost or client/server crash.'
elif code == '404':
print 'PCoIP connection not established due to inability to use the VMware video driver on the server.'
elif code == '405':
print 'PCoIP connection not established due to client and server not having a common encryption method enabled.'
elif code == '406':
print 'Zero Client and View 4.5 and earlier: PCoIP session terminated due to network issues on TCP/SSL connection. Zero client and View 4.6 (and later). This is normal operation since the TCP/SSL connection is terminated right after session negotiation.'
elif code == '407':
print 'PCoIP connection not established due to the View Security Server detecting that AES encryption is disabled on either the client and/or server.'
else:
print 'code not found in KB.'
return 0;
for root, dirs, files in os.walk("/users/home10/tshrestha/brb-view/logs/vdm-sdct-agent/pcoip-logs"):
lineContainsServerFile = re.compile('.*server.*')
for filename in files:
if lineContainsServerFile.match(filename):
filename = os.path.join(root,filename)
with open(filename,'rb') as filehandle:
function_pcoip_packetloss(filehandle);
filehandle.seek(0)
function_pcoip_decrease(filehandle);
filehandle.seek(0)
function_disconnect_codes(filehandle);
#variable defined to check for certain pattern or word in the file
lineContains_os_name = re.compile('.*Operating System.*')
lineContains_host_name = re.compile('.*Machine name.*')
lineContains_installed_memory = re.compile('.*Total Physical Memory.*')
lineContains_Vcpu = re.compile('.*Processor.*')
lineContains_java_heap_key_string = re.compile('.*JvmOptions.*')
lineContains_java_heap_key = re.compile('.*TomcatService.*')
lineContains_network_adapter = re.compile('.*Adapter.*')
lineContains_firewall_status = re.compile('.*firewall.*')
#opening a file to write the output of the script
with open("/users/home10/tshrestha/brb-view/logs/vdm-sdct-agent/computer-config/view_outputfile",'w') as file_open:
sys.stdout = file_open
with open("/users/home10/tshrestha/brb-view/logs/vdm-sdct-agent/computer-config/dxdiag.txt", 'rb') as files: #open the input file to read from it
for filename in files: #loops to go through the file line by line
if lineContains_os_name.match(filename): #checks if the file has Operating System name
operating_system = filename.strip(' ') #format the output
print operating_system #print the name of OS
if "Windows 7 Service Pack 1" in filename: #checks if it is Service Pack 1 and print the required KB no.
print "The ", operating_system ,"Please refer to the Microsoft Knowledge Base article 2550978"
else:
print "The ",operating_system, "Please refer to the Microsoft Knowledge Base article 2344941\n"
if lineContains_host_name.match(filename): #looks for the host name
host_name = filename.strip('')
print '\n'
print host_name
#opening the input file to get the name of network adapter
with open("/users/home10/tshrestha/brb-view/logs/vdm-sdct-agent/computer-config/ipconfig-all.txt", 'rb') as filename1: #open the input file to read from it
for line1 in filename1:
if lineContains_network_adapter.findall(line1): #looks for network adaptor
network_adapter = line1.strip('')
print network_adapter
#opening the input file system-info to get the number of virtual cpu and installed physical memomry
with open("/users/home10/tshrestha/brb-view/logs/vdm-sdct-agent/computer-config/systeminfo.txt", 'rb') as filename2: #open the input file to read from it
for line2 in filename2:
if lineContains_Vcpu.findall(line2): #looks for number of Virtual CPU
no_vcpu = line2.strip('')
print no_vcpu
if lineContains_installed_memory.findall(line2): #looks for installed physical memory
installed_memory = line2.strip('')
print installed_memory
print '-----------------'
print 'JAVA HEAP KEY'
print '-----------------'
with open("/users/home10/tshrestha/brb-view/loginfo_outputfile", 'w') as outputfilename:
sys.out = outputfilename
with open("/users/home10/tshrestha/brb-view/logs/vdm-sdct-cs/vdm-config/vmware-reg.txt", 'rb') as filename3: #open the input file to read from it
for line3 in filename3:
if lineContains_java_heap_key_string.findall(line3): #looks for Java Heap Keys
heap_key_string = line3.strip('')
print heap_key_string
if lineContains_java_heap_key.findall(line3):
java_heap_key = line3.strip('')
print java_heap_key
name = getpass.getuser() #assigning a variable to the current user
msg = {}
msg["From"] = name + "@vmware.com" #assigning a variable to the current user as a Sender
msg["To"] = name + "@vmware.com" #assigning a variable to the current user as a Recei
msg["Subject"] = "LOG INFO" #assining a variable to the subject field
#using bash script in python; sendmail; subject to and from and the outputfile
os.system("/usr/bin/mail -s " + msg["Subject"] + " " + msg["To"] +" < outputfilename")
答案 0 :(得分:0)
将sys.stdout分配给file_open,并在with语句的末尾关闭file_open。 print 将其输出发送到sys.stdout,但它现在是已关闭的文件。如果你想写文件,只需坚持使用file_open.write(“字符串”),不要重定向stdout。
查看代码,我认为 ipconfig-all.txt 和 systeminfo.txt 的'with'块应该缩进到它们包含在输出文件的'with'块。