Python编写文本文件

时间:2016-02-16 19:21:10

标签: python python-2.7 python-3.x

所以,这就是我想要做的......

error = driver.find_element_by_xpath("""//*[@id="MemberNameError"]""")
if error.is_displayed():
    print(line.strip() + " is taken")
else:
    print(line.strip() + " is available")
with open("emails_available.txt", "a") as email_txt:
    email_txt.write(line.strip() + "@hormail.com" + "\n")

如果电子邮件可用,我该如何制作,然后写入txt

1 个答案:

答案 0 :(得分:1)

如何将if / else放在with?

error = driver.find_element_by_xpath("""//*[@id="MemberNameError"]""")
with open("emails_available.txt", "a") as email_txt:
   if error.is_displayed():
      print(line.strip() + " is taken")
   else:
      print(line.strip() + " is available")
      email_txt.write(line.strip() + "@hormail.com" + "\n")

我认为这里可能存在一个循环,你没有包括......也可以进入with语句。