简体   繁体   English

请帮助:AttributeError: 'str' object 没有属性 'write'

[英]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_FILE = "password_log_Nicholas_Webb.txt"

So PASSWORD_LOG_FILE is a string.所以PASSWORD_LOG_FILE是一个字符串。
It is not a file object, it can not perform read or write operations.它不是file object,它不能执行读或写操作。
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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:&#39;str&#39;对象没有属性&#39;attack&#39;“&#39;Help!” - AttributeError: 'str' object has no attribute 'attack' “'Help!” AttributeError:&#39;str&#39;对象没有属性&#39;write&#39; - AttributeError: 'str' object has no attribute 'write' AttributeError:&#39;str&#39;对象没有属性 - AttributeError: 'str' object has no attribute AttributeError&#39;str&#39;对象没有属性 - AttributeError 'str' object has no attribute 最新错误= AttributeError:&#39;str&#39;对象没有属性&#39;write&#39; - Latest error = AttributeError: 'str' object has no attribute 'write' python pickle给出“AttributeError:'str'对象没有属性'write'” - python pickle gives “AttributeError: 'str' object has no attribute 'write'” 这是什么意思:AttributeError:&#39;str&#39;对象没有属性&#39;write&#39; - What does this mean: AttributeError: 'str' object has no attribute 'write' 为什么它运行我这个错误:AttributeError:'str' object 没有属性'write'? - why is it running me this Error: AttributeError: 'str' object has no attribute 'write'? Python 日志记录模块 AttributeError: 'str' object 没有属性 'write' - Python logging module AttributeError: 'str' object has no attribute 'write' AttributeError: &#39;str&#39; 对象没有属性 &#39;write&#39; - 将应用程序答案打印到 Excel 中 - AttributeError: 'str' object has no attribute 'write' - Print application answers into Excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM