How do I test if a custom made password (in a text file) is alphanumerical?

Viewed 37

So, basically im trying to create a code that reads lines from a text file (lets say the text file is named "passwords.txt") and tests if the password is alphanumerical. If it is alphanumerical, the code will print out "[the password from the line] is a valid password" and if not it will print out "[password from the line] contains invalid characters." ?

I have written some code example.


file = open("passwords.txt", "w")
file.write("5345m345ö34l\nno2no123non4\nnoq234n5ioqw#%\n%#""SGMSGSER\ndoghdp5234\nsg,dermoepm\n43453-frgsd\nhsth()))\nbmepm35wae\nvmopaem2234+0+\ngsdm12313")

file.close()

Like you see, theres absolutely nothing made in the code, thats because I have no idea how im supposed to continue it to make it do what I was asking. As you can see, at the end of the passwords there is a \n to make it easier for isalnum() (if that is what tells if its alphanumerical) but I think it has to be removed before the isalnum() reads it, I dont even know if its possible.

1 Answers

isalnum() returns true if a string contains alphanumeric characters, without symbols.

full code will be like

with open("file.txt") as file_in:
    lines = []
    for line in file_in:
        validity='INVALID'
        if(line.rstrip().isalnum()):
            validity='VALID'
        print(f'{line.rstrip()} IS {validity}')

the above code will take each line from txt file, remove white space ,check wheather the line is alnum or not and print the line

file.txt

hello
hello2
heelo1
hello1!@

OUTPUT

hello IS VALID
hello2 IS VALID
heelo1 IS VALID
hello1!@ IS INVALID
Related