You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于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 like JohnCitizen or JOHNSPASSWORD will still match the stored johncitizen johnspassword.
  • Safe File Handling: The with statement 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

  1. Create a text file named users.txt (or adjust the credentials_file path in the script) and add your user pairs (one per line, username followed by password separated by a space).
  2. Run the script, enter your username and password, and it will validate against the file.

内容的提问来源于stack exchange,提问作者Kieran Ryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:31:19