基于Python 3.6.x实现邮箱登录脚本的账号密码匹配验证
Hey there! Let's put together that email login script you need. Here's a straightforward implementation that handles file reading, case-insensitive credential checks, and user input validation:
Python Email Login Script with Case-Insensitive Validation
Understanding the File Setup
Your text file should have one username-password pair per line, separated by a space (e.g.,
johncitizen johnspassword). Empty lines are skipped automatically in the script below.
Complete Script Implementation
Here's a ready-to-use script with comments explaining each part:
def load_user_credentials(file_path): """Loads username-password pairs from a text file, stored in lowercase for case-insensitive checks.""" user_data = {} try: # Use 'with' to safely open/close the file automatically with open(file_path, 'r') as f: for line in f: # Clean up the line and skip empty ones cleaned_line = line.strip() if not cleaned_line: continue # Split into username and password (handles passwords with spaces using maxsplit=1) username, password = cleaned_line.split(maxsplit=1) # Store both as lowercase to enable case-insensitive matching user_data[username.lower()] = password.lower() return user_data except FileNotFoundError: print(f"Oops! Couldn't find the credentials file at {file_path}") return {} def login_flow(): # Update this path to match where your credentials file is stored credentials_file = "users.txt" users = load_user_credentials(credentials_file) if not users: print("No user accounts found. Please check your credentials file.") return # Get user input entered_username = input("Enter your username: ").strip() entered_password = input("Enter your password: ").strip() # Convert input to lowercase for case-insensitive comparison entered_username_lower = entered_username.lower() entered_password_lower = entered_password.lower() # Check if credentials match if entered_username_lower in users and users[entered_username_lower] == entered_password_lower: print("Login successful! Welcome in.") else: print("Invalid username or password. Please try again.") # Run the login flow when the script is executed directly if __name__ == "__main__": login_flow()
Key Features Explained
- Case Insensitivity: By converting both stored credentials and user input to lowercase with
.lower(), inputs likeJohnCitizenorJOHNSPASSWORDwill still match the storedjohncitizen johnspassword. - Safe File Handling: The
withstatement ensures the file is properly closed after reading, and we handle the case where the file is missing with a friendly error message. - Flexible Passwords: Using
split(maxsplit=1)means your passwords can include spaces if you ever need that functionality later.
How to Use It
- Create a text file named
users.txt(or adjust thecredentials_filepath in the script) and add your user pairs (one per line, username followed by password separated by a space). - Run the script, enter your username and password, and it will validate against the file.
内容的提问来源于stack exchange,提问作者Kieran Ryan
相关产品推荐
相关产品推荐

