Aoxx Aoxx - 1 month ago 6
Python Question

Simple program to append *** to a line in a list of users

I am trying to write a simple program that will read a list of users in my AD group and put "***" after a name if its new. It doesnt seem to be working write so I am just wondering where I am going wrong. here is what I have:

list1 = open('testlist.txt', 'r')

for i in list1:
print(i, end=' ')


list2 = open('NewUserList.txt', 'r+')

for ii in list2:
if ii not in list1:
list2.write(ii.strip("\n")+" ***\n")


Example of list1:

Mike Smith
John Smith
Tom Smith


Here is an example of what I am getting in list2 when its done:

Mike Smith
John Smith
Tom Smith
Jerry Smith
Mike Smith ***
John Smith ***
Tom Smith ***
Jerry Smith ***


Obviously this is showing that existing users are new users along with the new user.... What did I do wrong?

Answer
for i in list1:

list yields the lines, but doesn't store them. You have to keep the values yourself:

s = set()

with open('testlist.txt') as f:
     for line in f:
        s.add(line)