AttributeError:'_io.TextIOWrapper'对象没有属性'append'

时间:2018-08-03 09:46:31

标签: python

我一直在尝试使用append属性将内容添加到我的文本文档中,但是目前无法正常工作...有什么想法吗?

MyFile = open("usernames_passwords.txt", "a")
print("To sign up you will just need to answer the following questions.")
Email = input("Please enter your Email")
userUsername = input("Please enter a valid username")
password = input("please enter a password now: ")

userPasswordcheck = input("Please re-enter that password")

if password == userPasswordcheck:
    password_b = password.encode('utf-8')
    password_b_bytes = base64.b64encode(password_b)
    password_b_bytes_re = password_b_bytes.decode('utf-8')
    date_of_birth = input("please enter your date of birth (DD/MM/YY)")
    phone_num = input("please Enter your phone number for extra security (Type '!' if you dont want to enter one)")
    if phone_num == "!":
        phone_num = "_noneGiven_"

    MyFile.append(userUsername)
    MyFile.append(",")
    MyFile.append(password_b_bytes_re)
    MyFile.append(",")
    MyFile.append(Email)
    MyFile.append(",")
    MyFile.append(date_of_birth)
    MyFile.append(",")
    MyFile.append(phone_num)
    MyFile.close()

错误:

  

AttributeError:'_io.TextIOWrapper'对象没有属性'append'

1 个答案:

答案 0 :(得分:0)

尝试:

print("To sign up you will just need to answer the following questions.")
Email = input("Please enter your Email")
userUsername = input("Please enter a valid username")
password = input("please enter a password now: ")
userPasswordcheck = input("Please re-enter that password")

if password == userPasswordcheck:
    with open(filename, "a") as infile:           #Open File in Append Mode
        password_b = password.encode('utf-8')
        password_b_bytes = base64.b64encode(password_b)
        password_b_bytes_re = password_b_bytes.decode('utf-8')
        date_of_birth = input("please enter your date of birth (DD/MM/YY)")
        phone_num = input("please Enter your phone number for extra security (Type '!' if you dont want to enter one)")
        if phone_num == "!":
            phone_num = "_noneGiven_"
        to_write = [userUsername, password_b_bytes_re, Email, date_of_birth, phone_num]
        infile.write(", ".join(to_write) + "\n")     #Append Content