0

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()
6
  • What is someOutput? It's not defined anywhere Commented May 27, 2018 at 8:16
  • You are opening the file in "write" mode in the for loop, which will overwrite whatever text you have already written in the file in each iteration of the loop. Please use mode a instead as @Hans suggested.! Commented May 27, 2018 at 8:25
  • @Vinny .. i have edited the actual code tried, but while doing it it does not write the actual OutStr data into file Commented May 27, 2018 at 11:02
  • @JayJoshi .. mistakenly i kept +w. Commented May 27, 2018 at 11:02
  • @reculseSoul, did changing the mode solve the issue.? Commented May 28, 2018 at 6:01

2 Answers 2

1

As Vinny mentioned, this example is neither complete nor verifiable. My best guess is that you want to replace mode='w+' with mode='a'. You don't seem to be actually reading fileData, so don't bother with the +. Write mode overwrites your file every time you run through the loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Hans Musgrave, I have edited the real code tried , just to see what wrong i'm doing..
0

@reculseSoul, Use str.format method and close the fileWrite.close() outside the for loop. Please try below it should work.

def ChkCon():
    Flag    = False
    FlagTcp = False
    NewStr  = ''
    OutStr  = ''
    HOST    = ''
    MARKER  = '----------------'
    NEWLINE = '\n'
    fileData = 'ldap_conn_logs'
    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, 'w')
                fileWrite.write(OutStr)
                file.write('{0}'.format(OutStr))
        fileWrite.close()
        log.close()
ChkCon()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.