import os

def CountFrequency(my_list): 
    # Creating an empty dictionary  
    freq = {} 
    # Updating the value of the count if the operation exists 
    for item in my_list: 
        if (item in freq): 
            freq[item] += 1
    # Adding the operation to the dictionary if it doesn't exist 
        else: 
            freq[item] = 1
    return freq

#Fucntion to return a list path of all the files in a folder 
def ListOffiles(path):
    dirListing = os.listdir(path)
    return dirListing

def GetFrequencies(path):
    listOfFiles = ListOffiles(path)
    # Iterating over all files 
    for items in listOfFiles:
        if items.endswith('.txt'):
            filepath = path + '/' + items
            my_list = [] 
            #Open the cuurent file
            with open(filepath) as fp:
                line = fp.readline()
                cnt = 1
                while line:
                    line = fp.readline()
                    #Store the operation in the line to a list
                    my_list.append(line.rstrip('\n'))
                    cnt += 1
            #Delete last element if it is empty i.e empty last line
            if(my_list[-1]) == "":
                del my_list[-1]
            total_lines = cnt  
            #Count frequency of each operation in the file
            freq={}
            freq=CountFrequency(my_list)
            #Find percentage from the counted frequencies 
            for key, value in freq.items(): 
                freq[key] = (value/total_lines)*100
            #Display the percentages
            for key, value in freq.items(): 
                print ((key, value))

# Driver function 
if __name__ == "__main__": 
    #Path of the 1st directory containing all the malware samples
    path1 = "/Users/aryan/Desktop/MalwareNeuralNetwork-master/zbot"
    GetFrequencies(path1)
    print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
    #Path of the 2nd directory containing all the malware samples
    path2 = "/Users/aryan/Desktop/MalwareNeuralNetwork-master/zeroaccess"
    GetFrequencies(path2)