Check if the content already exists in the text file

Asked

Viewed 195 times

1

I would like to know how to check a TXT file and if you have a line like it does not add the content again, otherwise it adds. I tried to do so:

Tokens is an array with several strings, for example:

['Nzc3NzA2MTc2MzA4NzA3Mzc5.X7HVkg.v85rDccvWP-HJJxD_SMonOu', 'Nzc3NzA3ODI2Njg0MjMxNzEy.X7HXBw.CvxmjqeS8sW9Rx1sEy2ESLZ']

I would like to check this array, and on each item in the list it checks if it already has this token in the notepad, and if it doesn’t have it adds, if it already has it, you don’t need to do anything but print that it already exists:

for i in range(0, len(tokens)): 
        print(str(tokens[i]))
        with open('tokens.txt', "rt") as f:
            datafile = f.readlines()
            for line in datafile:
                if str(tokens[i]) in line:
                    print("Já possue esse token")
                else:
                    with open('tokens.txt', "w") as f:
                        f.write(str(tokens[i]) + '\n')
                        message = str(tokens[i])

However it is not working, the variable "message" always receives the token that was already written in . TXT, what should I do?

  • What is this variable tokens? You can edit your question by adding this information?

  • 1

    It is to check each string of the array in each row, and if the string is not present in any line of the . txt, the string must be written at the end of the right file?

1 answer

1

Sample tokens:

tokens = [
    'Nzc3NzA2MTc2MzA4NzA3Mzc5.X7HVkg.v85rDccvWP-HJJxD_SMonOu', 
    'Nzc3NzA3ODI2Njg0MjMxNzEy.X7HXBw.CvxmjqeS8sW9Rx1sEy2ESLZ',
    'Nzc3NzA3ODI2Njg0MjMxNzEy.X7HXBw.CvxmjqeS8sW9Rx1sEy2ESLj',
    'Nzc3NzA3ODI2Njg0MjMxNzEy.X7HXBw.CvxmjqeS8sW9Rx1sEy2ESpl'
]

Open file in read mode

with open('./tokens.txt','r') as reader:
    tokens_reader = reader.readlines()

Removing the n(line break)

tokens_reader = [item.replace('\n','') for item in tokens_reader]

Opening the file in written mode:

with open('./tokens.txt','a') as writer:
    writer.write('\n')
    writer.write('\n'.join(set.difference(set(tokens), set(tokens_reader))))

Note that this line is the one that does the work to check if there is equal content and save it to the file if it does not exist

writer.write('\n'.join(set.difference(set(tokens), set(tokens_reader))))

We compare script tokens with file tokens and return this difference

set.Difference:

The difference between the two sets in Python is equal to the difference between the number of elements in two sets. The Difference() function returns a set that is the difference between two sets.


Entire code:

with open('./tokens.txt','r') as reader:
    tokens_reader = reader.readlines()

tokens_reader = [item.replace('\n','') for item in tokens_reader]

with open('./tokens.txt','a') as writer:
    writer.write('\n')
    writer.write('\n'.join(set.difference(set(tokens), set(tokens_reader))))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.