python

Script para retirar emails duplicados

Fiz um script para retirar emails duplicados de uma lista, colocar estes emails em ordem alfabética e retirar os emails que constam em uma lista de exclusão.

Talvez seja problemático o fato do script ter sido feito com a sintaxe do python 3 🙁

O script deve rodar em uma pasta que contenha um arquivo mailing.txt com uma lista de emails e outro arquivo mailing opt-out.txt com lista de emails que devem ser retirados da lista principal. Estes arquivos devem ter um endereço por linha.

O script:

#!/usr/bin/python
import datetime, os, sys
print("Removing double entries in mailing and opt-outs")
 
t = datetime.datetime.now()
timeStamp = str(t.today()).replace(':', '-')
fMailing = 'mailing.txt'
fMailingOut = 'mailing opt-out.txt'
backupFolder = 'mailing-backup' + os.sep
fMailingBackup = backupFolder + fMailing + '.' + timeStamp + '.backup'
fMailingOutBackup = backupFolder + fMailingOut + '.' + timeStamp + '.backup'
 
try:
  fMailingHandle = open(fMailing)
  fMailingList = fMailingHandle.readlines()
except IOError:
  print('Put a file named ' + fMailing + ' in this folder before running me.')
  sys.exit()
 
try:
  fMailingOutHandle = open(fMailingOut)
  fMailingOutList = fMailingOutHandle.readlines()
except IOError:
  print('Put a file named ' + fMailingOut + ' in this folder before running me.')
  sys.exit()
 
lstEmail = []
lstOptOut = []
 
for email in fMailingList:
  lstEmail.append(email.lower())
 
for email in fMailingOutList:
  lstOptOut.append(email.lower())
 
fMailingHandle.close()
fMailingOutHandle.close()
 
#transform in a set to make each email unique
print("Original size: mailing=", len(lstEmail), ", out-out=", len(lstOptOut))
lstEmail = set(lstEmail)
lstOptOut = set(lstOptOut)
 
for email in lstOptOut:
  try:
    lstEmail.remove(email)
  except KeyError:
    #print("email da lista de out-out não existe no mailing: tudo bem!")
    pass
 
print("Actual size: malling=", len(lstEmail), ", out-out=", len(lstOptOut))
print("Saving results in files... I'll keep some backup files (with timestamps).")
 
if os.path.exists(backupFolder):
  print('backup folder exist')
else:
  os.mkdir(backupFolder)
  print('bakckup folder does not exist')
 
print(fMailing, ", " , fMailingBackup)
os.rename(fMailing, fMailingBackup)
os.rename(fMailingOut, fMailingOutBackup)
 
#save the opt-out list ina a file
fMailingOutHandle = open(fMailingOut, 'w')
for email in sorted(lstOptOut):
  fMailingOutHandle.write(email)
fMailingOutHandle.close()
 
#save the mailing in a file
fMailingHandle = open(fMailing, 'w')
for email in sorted(lstEmail):
  fMailingHandle.write(email)
fMailingHandle.close()
 
print("Ready, done :)")