Python Cyber Security - Check and print valid passwords from a file
Python Cyber Security: Exercise-5 with Solution
Write a Python program that reads a file containing a list of passwords, one per line. It checks each password to see if it meets certain requirements (e.g. at least 8 characters, contains both uppercase and lowercase letters, and at least one number and one special character). Passwords that satisfy the requirements should be printed by the program.
password.txt:
Pas1$Ku1 password password123 password123$ Password6#(% Germany#12 USA12^$# England0#
Sample Solution:
Python Code:
import re
def check_password(password):
# Define regular expressions for each requirement
length_regex = re.compile(r'^.{8,}$')
uppercase_regex = re.compile(r'[A-Z]')
lowercase_regex = re.compile(r'[a-z]')
digit_regex = re.compile(r'\d')
special_regex = re.compile(r'[\W_]')
# Check if password meets each requirement
length_check = length_regex.search(password)
uppercase_check = uppercase_regex.search(password)
lowercase_check = lowercase_regex.search(password)
digit_check = digit_regex.search(password)
special_check = special_regex.search(password)
# Return True if all requirements are met, False otherwise
if length_check and uppercase_check and lowercase_check and digit_check and special_check:
return True
else:
return False
# Open file containing passwords
with open('passwords.txt') as f:
# Read each password from file and check if it meets requirements
for password in f:
password = password.strip() # Remove newline character
if check_password(password):
print("Valid Password: "+password)
else:
print("Invalid Password: "+password)
Sample Output:
Valid Password: Pas1$Ku1 Invalid Password: password Invalid Password: password123 Invalid Password: password123$ Valid Password: Password6#(% Valid Password: Germany#12 Invalid Password: USA12^$# Valid Password: England0#
Flowchart:

Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Function to suggest character substitutions for stronger passwords.
Next: Check if passwords in a list have been leaked in data breaches.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Decapitalizes the first letter of a string:
Example:
def tips_decapitalize(s, upper_rest=False): return s[:1].lower() + (s[1:].upper() if upper_rest else s[1:]) print(tips_decapitalize('PythonTips')) print(tips_decapitalize('PythonTips', True))
Output:
pythonTips pYTHONTIPS
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook