Brendan Rodgers
Brendan Rodgers

Reputation: 305

Python - Read each line of text file and pass each line to variable

I am attempting to implement a simple password checker which will allow the user to open a text file that contains generated passwords and check them against a list of criteria to see if the password is strong or weak. It should then output the password with the result of whether it is strong or weak.

With the current solution I can get the list of passwords to print, but when the check as to whether the password is strong or not, only one result is shown. I am attempting for the output to be the password that is generated with the result e.g. Randompassword123 - This is a weak password.

Below is the code I am currently using:

def multiplestrength(): # password strength check function 
        textfile =  filedialog.askopenfilename(initialdir="/home", title = "Select text file to split",filetypes = (("text files","*.txt"),("all files","*.*")))
        
     
        with open(textfile , mode="r",encoding="utf-8") as my_file:
            data=my_file.read()
            print(data)
        
        def strongPassword(data):
            
                if passRegex1.search(data) == None:
                    return False
                if passRegex2.search(data) == None:
                    return False
                if passRegex3.search(data) == None:
                    return False
                if passRegex4.search(data) == None:
                    return False
                else:
                    return True

        passRegex1 = re.compile(r'\w{8,}')
        passRegex2 = re.compile(r'\d+')
        passRegex3 = re.compile(r'[a-z]')
        passRegex4 = re.compile(r'[A-Z]')

        


        if strongPassword(data) == True:
            print("Strong Password")
        else:
            print("This is not a strong password")
            

The output I receive is below

Output received

So it appears that only one password is being checked from the list of 5 passwords in the text file. I believe there might need to be a for loop required somewhere for each password to be checked, but I am unsure what way to approach this. Another approach I was thinking would be to insert the passwords from the text file into a list and iterating through this list to get a result for each password. Does this possibly sound like the correct way to approach this?

Any help with this would be greatly appreciated.

Thanks

Upvotes: 2

Views: 1581

Answers (2)

milanbalazs
milanbalazs

Reputation: 5339

I have written a solution. I have added many comment for code as description.

Code:

import re

passRegex1 = re.compile(r'\w{8,}')
passRegex2 = re.compile(r'\d+')
passRegex3 = re.compile(r'[a-z]')
passRegex4 = re.compile(r'[A-Z]')


def strong_password(data):  # the function name should be snake case
    if not passRegex1.search(data):  # Use "if not". It is the Pythonic way. It handles False/None/0/""/[] etc...
        return False
    if not passRegex2.search(data):
        return False
    if not passRegex3.search(data):
        return False
    if not passRegex4.search(data):
        return False
    else:
        return True


with open("test.txt", mode="r", encoding="utf-8") as my_file:  # Open file for reading
    for line in my_file.readlines():  # Read all lines one-by-one
        print("\nPassword: {}".format(line.strip()))  # Print the current password ("strip" removes the whitespace characters from string).
        if strong_password(line):  # This statement is True if the "strong_password" function returns True
            print("Strong Password")  
            continue  # Get the next element (line of file)
        print("This is not a strong password")  # Else statement is not needed because the "if" contains a continue

My test file:

asdf
121234
adsf123
asdffdsatre323423
fdggfd2323____,,,**
tt
333345
asdfSDFGRAdsfAERTGRghRGads___++((((FDsaSDfAS4233423524
434
55555

Output:

>>> python3 test.py

Password: asdf
This is not a strong password

Password: 121234
This is not a strong password

Password: adsf123
This is not a strong password

Password: asdffdsatre323423
This is not a strong password

Password: fdggfd2323____,,,**
This is not a strong password

Password: tt
This is not a strong password

Password: 333345
This is not a strong password

Password: asdfSDFGRAdsfAERTGRghRGads___++((((FDsaSDfAS4233423524
Strong Password

Password: 434
This is not a strong password

Password: 55555
This is not a strong password

Upvotes: 2

ynotzort
ynotzort

Reputation: 336

You could loop through the lines of your file using:

with open(textfile , mode="r",encoding="utf-8") as my_file:
  for line in my_file:
    # do something with the line, eg:
    if strongPassword(line):
       # ...

Edit: you might want to use line.strip() instead of line to get rid of the newline characters at the end (\n)

Upvotes: 2

Related Questions