0

I am using windows10 and python 2.7.14. Running the python scripts in command prompt. I want to read some lines in text file and compare with some text, if it matches it should be stored in array. And also I want the array should be global. But In my script the I am not able to store the contents in array. How do I achieve this.

#This method is to reading logfile and saving the different datas in different lists
def Readlogs(Filename):
        datafile = file(Filename)
        for line in datafile:
            if "login = " in line:
                print(line)
                trial=line
                s2 = "= "
                ArrayLogin = trial[trial.index(s2) + len(s2):]
                print(ArrayLogin)
            print(ArrayLogin)
            if "Overlay = " in line:
                print(line)
                trial2=line
                s2 = "= "
                arrayOverlay = trial2[trial2.index(s2) + len(s2):]
                print(arrayOverlay)
    Readlogs(WriteFileName)
  • If you want to declare your variables outside function and use them inside, then probably you may get some insights here https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them – Vivek Harikrishnan Dec 26 '17 at 06:49

1 Answers1

0

You can declare empty arrays and append items to it.

#This method is to reading logfile and saving the different datas in different lists
def Readlogs(Filename):
        #empty array
        ArrayLogin, arrayOverlay  = [], []
        datafile = file(Filename)
        for line in datafile:
            if "login = " in line:
                print(line)
                trial=line
                s2 = "= "
                ArrayLogin.append(trial[trial.index(s2) + len(s2):])
                print(ArrayLogin)
            print(ArrayLogin)
            if "Overlay = " in line:
                print(line)
                trial2=line
                s2 = "= "
                arrayOverlay.append(trial2[trial2.index(s2) + len(s2):])
                print(arrayOverlay)

        return ArrayLogin, arrayOverlay

arr1, arr2, = Readlogs(WriteFileName)
Rakesh
  • 81,458
  • 17
  • 76
  • 113