Новое решение:
tempPath = input("Please Enter the Path of the File\n")
temp_file = open(tempPath, "r")
fileContent = temp_file.read()
temp_file.close()
pattern_normal = re.compile("[-a-zA-Z0-9._][email protected][-a-zA-Z0-9_]+.[a-zA-Z0-9_.]+")
addresses = list(set(pattern_normal.findall(str(fileContent))))
with open('new_emails.txt', 'a+') as f:
f.write('\n'.join(addresses))
Я думаю, что ваша логика была неправильной, это работает:
addresses = ['[email protected]', '[email protected]']
with open('emails_file.txt', 'a+') as f:
fdata = f.read()
for mail in addresses:
if not mail in fdata:
f.write(mail + '\n')
Не прочитав много кода,
похоже, что вы зацикливаете строки по очереди, проверяя, существует ли в строке адрес, который вы также зацикливаете, если вы не добавляете к нему свой адрес электронной почты? Но в 99% из 100 строк адрес не будет в строке, поэтому вы получите нежелательное дополнение.
Вывод моего фрагмента кода:
[[email protected] ~]$ cat emails_file.txt
[email protected]
[email protected]
[[email protected] ~]$ python test.py
[[email protected] ~]$ cat emails_file.txt
[email protected]
[email protected]
[email protected]
[[email protected] ~]$