如何使用文件写入输入和日期和时间

问题描述 投票:0回答:2

我正在尝试学习如何使用while循环来记录每个不正确的尝试,包括时间和日期以及原因。输出还应显示文件的错误/弱尝试。

MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 14
PASSWORD_LOG_FILE = "password_log_your_name.txt"
password = input("Enter your password: ")
password_length = len(password)


while password_length >= MIN_PASSWORD_LENGTH and password_length <= MAX_PASSWORD_LENGTH:

    PASSWORD_LOG_FILE = open("password_log_your_name.txt", "a")

    PASSWORD_LOG_FILE.write(password)
    PASSWORD_LOG_FILE.write("\n")

    import datetime

    my_date = datetime.datetime.today()

    print(str(datetime.datetime.today()))
    todays_date = my_date.strftime('%A %B %d, %Y')

    print(f"Date: {todays_date:s}\n")

    PASSWORD_LOG_FILE.close()

    if password.isalpha():
        print("Your password is weak! It only contains letters.")
    elif password.isnumeric():
        print("Your password is weak! It only contains numbers.")
    else:
        print("Your password is strong! It contains letters and numbers.")
    break

我不确定顺序应该如何?我知道我需要使用datetime记录他们尝试尝试的时间并使用.write写入文件。无论如何我想,但我很困惑如何结合一切。

python
2个回答
1
投票

考虑到你需要存储所有的尝试,无论是week还是strong,我建议使用while True并按以下顺序:

import datetime
ps_file = "list.txt"

while True:
    p_inp = input("Enter your password OR Press N to quit")
    if p_inp.lower() == 'n':
        exit()
    with open(ps_file, 'a+') as fileObj:
        if p_inp.isalpha():
            print("Your password is weak! It only contains letters.")
            p_strength = "weak"
        elif p_inp.isnumeric():
            print("Your password is weak! It only contains numbers.")
            p_strength = "weak"
        else:
            print("Your password is strong! It contains letters and numbers.")
            p_strength = "strong"

        fileObj.write("Date: {}".format(datetime.datetime.today()) + "\n")
        fileObj.write("Password: {}".format(p_inp + "\n"))
        fileObj.write("Password strength: {}".format(p_strength) + "\n")

OUTPUT(list.txt):

Date: 2019-03-27 11:41:10.107696
Password: weekattempt
Password strength: weak
Date: 2019-03-27 11:41:14.388402
Password: strongattempt1
Password strength: strong
Date: 2019-03-27 11:41:26.812254
Password: strongatempt2
Password strength: strong

编辑:

如果您使用单独的函数进行密码验证和文件写入会更好:

import datetime

ps_week = "WEEK"
ps_strong = "STRONG"

ps_week_a = "Your password is weak! It only contains letters."
ps_week_b = "Your password is weak! It only contains number."
ps_strong_a = "Your password is strong! It contains letters and numbers."


def writeTofile(fileObj, p_strength, p_text):
    fileObj.write("Date: {}".format(datetime.datetime.today()) + "\n")
    fileObj.write("Password: {}".format(p_inp + "\n"))
    fileObj.write("Password strength: {}".format(p_strength) + "\n")
    fileObj.write("Prompt: {}".format(p_text) + "\n")

def p_validate(p_inp, fileObj):
    if p_inp.isalpha():
        writeTofile(fileObj, ps_week, ps_week_a)
    elif p_inp.isnumeric():
        writeTofile(fileObj, ps_week, ps_week_b)
    else:
        writeTofile(fileObj, ps_strong, ps_strong_a)

while True:
    p_inp = input("Enter your password OR Press N to quit")
    if p_inp.lower() == 'n':
        exit()
    with open('list.txt', 'a+') as fileObj:
        p_validate(p_inp, fileObj)

OUTPUT:

Date: 2019-03-27 11:51:36.512665
Password: weakweak
Password strength: WEAK
Prompt: Your password is weak! It only contains letters.
Date: 2019-03-27 11:51:55.704586
Password: strongatttempt1
Password strength: STRONG
Prompt: Your password is strong! It contains letters and numbers.
Date: 2019-03-27 11:52:50.609155
Password: strongattempt2
Password strength: STRONG
Prompt: Your password is strong! It contains letters and numbers.

0
投票

不知道为什么你打算用这种方式做任何事情。但根据你的问题,这应该是顺序。您可能希望使用with open('filename','a') as PASSWORD_LOG_FILE:进行上下文处理来执行文件处理。这只是为了演示一系列做你想做的事情。试图使用你的大部分代码。

import datetime

MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 14
PASSWORD_LOG_FILE = "password_log_your_name.txt"
password = input("Enter your password: ")
password_length = len(password)


if password_length >= MIN_PASSWORD_LENGTH and password_length <= MAX_PASSWORD_LENGTH:
    if password.isalpha():
        print("Your password is weak! It only contains letters.")
    elif password.isnumeric():
        print("Your password is weak! It only contains numbers.")
    else:
        print("Your password is strong! It contains letters and numbers.")
        PASSWORD_LOG_FILE = open("password_log_your_name.txt", "a")
        PASSWORD_LOG_FILE.write(password)
        PASSWORD_LOG_FILE.write("\n")
        my_date = datetime.datetime.today()
        print(str(datetime.datetime.today()))
        todays_date = my_date.strftime('%A %B %d, %Y')
        PASSWORD_LOG_FILE.write(todays_date)
        PASSWORD_LOG_FILE.write("\n")
        print(f"Date: {todays_date:s}\n")
        PASSWORD_LOG_FILE.close()
© www.soinside.com 2019 - 2024. All rights reserved.