How to write a variable data info a file in case function is defined. While i'm trying below method but it just write few lines only:
Please let me know what mistake i'm doing here...
fileData = 'ldap_conn_logs'
def ChkCon():
Flag = False
FlagTcp = False
NewStr = ''
OutStr = ''
HOST = ''
MARKER = '----------------'
NEWLINE = '\n'
with open('ldap_bind_netstat', 'r') as log:
for line in log:
if '| SUCCESS |' in line:
if Flag:
return HOST
return MARKER
return NewStr
if '| SUCCESS |' in line:
Flag = False
FlagTcp = True
HOST = line.split()[0]
NewStr = ''
elif 'FAILED' in line:
FlagTcp = False
elif 'UNREACHABLE' in line:
FlagTcp = False
if FlagTcp and line.startswith('tcp'):
NewStr += line
if 'tsdj-lb-oidc01' not in line:
Flag = True
OutStr = HOST + NEWLINE + MARKER + NEWLINE + NewStr
fileWrite = open(fileData, mode='a', encoding='utf-8')
fileWrite.write(OutStr)
fileWrite.close()
ChkCon()
someOutput? It's not defined anywhereforloop, which will overwrite whatever text you have already written in the file in each iteration of the loop. Please use modeainstead as @Hans suggested.!OutStrdata into file+w.