文本文件附加在python中的问题

时间:2014-09-09 17:07:29

标签: python file text append

我遇到了python问题。

我已经制作了一个程序,要求用户提供他们的电子邮件地址,然后将其附加到文本文件中,经过一些检查后,一切正常,但最终文本文件中没有任何内容,即使没有错误出现。

我的代码是:

def main():
    print("Hello and welcome to customer email program!")

    count=0

    while count < 1:
        email=str(input("Email Address: "))
        if "@" in email:
            if email.islower == True:
                count=2
                with open("emails.txt", "a") as myfile:
                    myfile.write(email)
                print("File added to databse")
            else:
                email=email.lower()
                count=2
                with open("emails.txt", "a") as myfile:
                    myfile.write(email)
        else:
            print("That is not an email address, please try again.")
main()

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我认为你应该打开文件,然后在追加文件后关闭文件:

def main() :
    print("Hello and welcome to customer email program!")

    done = False

    while not done :
        email = str(input("What's your email address? "))
        if "@" in email :
            if not email.lower() == email :
                email = email.lower()
            done = True
            f = open("emails.txt" "a")
            f.write(email)
            f.close()
        else :
            print("Please type in a valid email address, "+email+" isn't a valid email address")
main()

这是否符合您的需求?