我编写Python脚本来验证跳动
from validate_email import validate_email
with open("test.txt") as fp:
line = fp.readline()
cnt = 1
while line:
line = fp.readline()
print ('this email :' + str(line) +'status : ' + str((validate_email(line,verify=True))))
stt=str(validate_email(line,verify=True))
email=str(line)
print ("-----------------")
cnt += 1
if stt == "True":
file=open("clean.txt",'w+')
file.write(email)
if stt == "None":
file=open("checkagain.txt",'w+')
file.write(email)
if stt == "False":
file=open("bounces.txt",'w+')
file.write(email)
对于False条件,它会创建文件,但是即使确保我有退回电子邮件,也不会在其中发送电子邮件
答案 0 :(得分:1)
您需要关闭文件以反映文件中的更改,并放置:
file.close()
最后
您应该使用:
with open('bounces.txt', 'a') as file:
# your file operations
那样您就不必关闭文件
答案 1 :(得分:0)
您的脚本包含许多错误。
这里是重构,内嵌有关更改的注释。
from validate_email import validate_email
# Open output files just once, too
with open("test.txt") as fp, \
open('clean.txt', 'w') as clean, \
open('checkagain.txt', 'w') as check, \
open('bounces.txt', 'w') as bounces:
# Enumerate to keep track of line number
for i, line in enumerate(fp, 1):
# Remove trailing newline
email = line.rstrip()
# Only validate once; don't coerce to string
stt = validate_email(email, verify=True)
# No need for str()
print ('this email:' + email +'status: ' + stt)
# Really tempted to remove this, too...
print ("-----------------")
# Don't compare to string
if stt == True:
clean.write(line)
elif stt == None:
check.write(line)
elif stt == False:
bounces.write(line)
您没有在任何地方使用行号,但我将其留在了上面以说明它通常是如何完成的。