简体   繁体   中英

please help :AttributeError: 'str' object has no attribute 'write'

I'm doing an assignment for my course and I keep getting error message of

AttributeError: 'str' object has no attribute 'write'

Could someone please help me understand what I have done wrong? I will list the code below and leave out the pseudocode.

def main():

    import datetime
    print("PasswordChecker3 program developed by: Nicholas Webb")

    MIN_PASSWORD_LENGTH = 6
    MAX_PASSWORD_LENGTH = 10
    PASSWORD_LOG_FILE = "password_log_Nicholas_Webb.txt"
    curr_date_and_time = datetime.datetime.today()

    password = input("Enter Password: ")
    password_len = len(password)

    while password_len < MIN_PASSWORD_LENGTH or password_len > MAX_PASSWORD_LENGTH:
        print(f"Your Password has {password_len} characters, Password length needs to be between {MIN_PASSWORD_LENGTH} and {MAX_PASSWORD_LENGTH} characters long.")
        open(PASSWORD_LOG_FILE, "a")
        if password_len < MIN_PASSWORD_LENGTH:
            print(f"password length is {password_len} characters long.")
            invalid_password_reason = "password < 6"
            PASSWORD_LOG_FILE.write("{a}, {b} characters".format(a=curr_date_and_time, b=invalid_password_reason))
            PASSWORD_LOG_FILE.write("\n")
        else:
            print(f"password length is {password_len} characters long.")
            invalid_password_reason = "password > 10"
            PASSWORD_LOG_FILE.write("{a}, {b} characters".format(a=curr_date_and_time, b=invalid_password_reason))
            PASSWORD_LOG_FILE.write("\n")

        password = input("re-enter password: ")
        password_len = len(password)
    PASSWORD_LOG_FILE.close()
    if password.isnumeric():
        print("Password weak - Contains only numbers")
    elif password.isalpha():
        print("Password weak - Contains only letters")
    else:
        print("Password strong")


main()

PASSWORD_LOG_FILE is initialized as

PASSWORD_LOG_FILE = "password_log_Nicholas_Webb.txt"

So PASSWORD_LOG_FILE is a string.
It is not a file object, it can not perform read or write operations.
You can not apply on it methods like

PASSWORD_LOG_FILE.write("{a}, {b} characters".format(a=curr_date_and_time, b=invalid_password_reason))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM