Sep-10-2023, 11:41 AM
(This post was last modified: Sep-10-2023, 11:41 AM by Pedroski55.)
If you look up the docs for datetime, you will find it allows you to format the output string in many ways. You can show all or just part of the info.
Open the log file 'a' to append to the existing file, but that will create the file if it is not there.
I think this will do something like what you want. Just some fun on Sunday morning!
Open the log file 'a' to append to the existing file, but that will create the file if it is not there.
I think this will do something like what you want. Just some fun on Sunday morning!
from datetime import datetime # in case you want to do time zone stuff from zoneinfo import ZoneInfo path2log = '/home/pedro/temp/password_log.txt' def logit(password): if len(password) < 6: reason = 'too short' elif len(password) > 10: reason = 'too long' elif password.isalpha() or password.isnumeric(): reason = 'weak' else: reason = 'strong' # only log unacceptable passwords if not reason == 'strong': dt = datetime.now() date = dt.strftime('%A %d-%m-%Y, %H:%M:%S.%f') with open(path2log, 'a') as mylog: mylog.write(f'Password: {password} attempted on {date} declined because it is {reason}. \n') return False else: print(f'{password} is a good {reason} password! You will be a secret agent someday!') return True acceptable = False while not acceptable: print('Please enter a password between 6 and 10 characters long.') print('Your password should contain at least 1 non-alphanumeric character.') password = input('Enter your password. It must be a minimum of 6 and a maximum of 10 characters and have at least 1 non-alphanumeric character. \n') acceptable = logit(password)