sherpaurgen
sherpaurgen

Reputation: 3274

Make python script to wait for return from module function

Im am learning python and trying to write a script to validate email domains if it exist with help from module by Syrus Akbary.I apologise if this is correct way to ask the question. This script will take "email list file" as input and the file will have emails in it. I have installed the module and tested parts of it in python shell but in script it wont work and runs without waiting and writing to file(too fast. in python shell it took 2,3 seconds).

Below is my code :

#!/bin/python
#python version used is 2.7.5
import sys
if len(sys.argv) != 2:
    print "Missing Email List File"
    exit()
else:
    email_listfile=sys.argv[1]
    with open(email_listfile,'r') as email:
    for line in email.readlines():
        print line
        status_mx=validate_email(line,check_mx=True)
        print status_mx
        if status_mx == True:
            with open('corrected-emails.txt','a') as truemail:
                truemail.write(line)
        else:
            pass

The email list file looks like this:

[email protected]
[email protected]
[email protected]

Below is output when i run the script

# ./test.py emaillist.txt 
[email protected]

False
[email protected]

False    
[email protected]

False

*when run in python shell

>>> from validate_email import validate_email
>>> status_mx = validate_email('[email protected]',check_mx=True)
>>> print status_mx
    True
#when given a non existing domain
>>> status_mx = validate_email('[email protected]',check_mx=True)
>>> print status_mx
False

Upvotes: 2

Views: 685

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90989

The issue is that you have a newline after the email . You can notice that when you print the email itself, the False status is getting printed with an empty line in between -

# ./test.py emaillist.txt 
[email protected]

False
[email protected]

False

This is what is most probably causing the issue with your library, you should send in the email after stripping off the extra newlines/whitespaces from the ends and beginning. Example -

else:
    email_listfile=sys.argv[1]
    with open(email_listfile,'r') as email:
    for line in email.readlines():
        print line
        status_mx=validate_email(line.strip(),check_mx=True)
        print status_mx

Upvotes: 1

Related Questions