I want to know how to write 'if' statement while checking email. If it is returning True and email is valid I want to write it to the text file comparing if the email is existing already duplicated in the save output file.
from validate_email import validate_email
import os
def email_validator():
global reqs, _lock, success, fails
with open(os.path.join("./emails.txt"), "r") as f:
for line in f:
line.strip()
print("checking email: " line)
is_valid = validate_email(
email_address=line,
check_format=True,
check_blacklist=False,
check_dns=True,
dns_timeout=10,
check_smtp=True,
smtp_timeout=10,
smtp_helo_host='my.host.name',
smtp_from_address='[email protected]',
smtp_skip_tls=False,
smtp_tls_context=None,
smtp_debug=False
)
if validate_email.__code__ == 200:
print(f'Email {is_valid} is valid : {success} ')
success = 1
with open("./validated_emails.txt", 'r ') as f:
valid_emails = f.read()
if line not in valid_emails:
f.write(line.strip() '\n')
else:
print("Not valid Email!")
fails =1
elif validate_email == False:
fails = 1
continue
CodePudding user response:
A couple of important misunderstandings to clear up first:
validate_email
is a function. To get the result of a function, we call it with()
following the function name. You already do this on line 11 withis_valid = validate_email(*args)
.
The variableis_valid
is now storing the result ofvalidate_email()
This is probably either True, False or None. I couldn't figure out exactly what module you're using for validation, as the validate_email module I installed from pip only has 5 parameters in the function definition.On line 27, you have
validate_email.__code__ == 200
. This will ALWAYS be False.
__code__
is an attribute of of thevalidate_email
function and represents the literal code of the function as a code object. It does not represent the last return value of the function.Likewise, on line 38, you have
if validate_email == False
. This will also ALWAYS be a false comparision.validate_email
is a object, and will never == False.
Here's my take on correcting your code, but without being able to verify the exact validate_email
module you are using, it might not be correct:
from validate_email import validate_email
import os
def email_validator():
global reqs, _lock, success, fails
with open(os.path.join("./emails.txt"), "r") as f:
for line in f:
line.strip()
print("checking email: " line)
is_valid = validate_email(
email_address=line,
check_format=True,
check_blacklist=False,
check_dns=True,
dns_timeout=10,
check_smtp=True,
smtp_timeout=10,
smtp_helo_host='my.host.name',
smtp_from_address='[email protected]',
smtp_skip_tls=False,
smtp_tls_context=None,
smtp_debug=False
)
if is_valid:
success = 1
print(f'Email {is_valid} is valid : {success} ')
with open("./validated_emails.txt", 'r ') as f:
valid_emails = f.read()
if line not in valid_emails:
f.write(line.strip() '\n')
else:
fails = 1
print(f"{line} is not a valid email : {fails}")
# continue # This is not needed when we're at the end of the loop