'python generator function not yielding output [closed]

import itertools
def password_generator():
    with open("D:\Password Hacker\Password Hacker\\task\passwords.txt", "r") as passfile:
        for word in passfile:
            if not word.isdigit():
                for var in itertools.product(*([letter.lower(), letter.upper()]for letter in word.strip("\n"))):
                    yield ("".join(var))
            else:
                yield (word.strip("\n"))

g = password_generator()
for i in range(10):
print(g.__next__()) #prints same result everytime...

The code generates all possible combinations of uppercase and lowercase letters in a word from the file passwords.txt. However, the code outputs same result in each iteration



Solution 1:[1]

try :

import itertools


def password_generator():
    with open("D:\Password Hacker\Password Hacker\\task\passwords.txt", "r") as passfile:
        for word in passfile:

            print('word : ', word , type(word), word.isdigit()) #you can comment out this line

            if not word.strip("\n").isdigit():
                for var in itertools.product(*([letter.lower(), letter.upper()]for letter in word.strip("\n"))):
                    yield ("".join(var))
            else:
                yield (word.strip("\n"))

g = password_generator()

  
try :  
    while g:
        print(g.__next__()) #prints same result everytime...
        
except:
    print('empty')

input passwords.txt file:

pippo
pluto
123456

output:

word :  pippo
 <class 'str'> False
pippo
pippO
pipPo
pipPO
piPpo
piPpO
piPPo
piPPO
pIppo
pIppO
pIpPo
pIpPO
pIPpo
pIPpO
pIPPo
pIPPO
Pippo
PippO
PipPo
PipPO
PiPpo
PiPpO
PiPPo
PiPPO
PIppo
PIppO
PIpPo
PIpPO
PIPpo
PIPpO
PIPPo
PIPPO
word :  pluto
 <class 'str'> False
pluto
plutO
pluTo
pluTO
plUto
plUtO
plUTo
plUTO
pLuto
pLutO
pLuTo
pLuTO
pLUto
pLUtO
pLUTo
pLUTO
Pluto
PlutO
PluTo
PluTO
PlUto
PlUtO
PlUTo
PlUTO
PLuto
PLutO
PLuTo
PLuTO
PLUto
PLUtO
PLUTo
PLUTO
word :  123456
 <class 'str'> False
123456
empty

you can get same result using:

import itertools


def count_pwd():
    with open("passwords.txt", "r") as passfile:
        
        cnt = 0
        
        for word in passfile.readlines():
            
            if not word.strip("\n").isdigit():
            
                cnt += 2**len(word)
                
            else:
                
                cnt += 1


    return cnt        
        
    

def password_generator():
    with open("D:\Password Hacker\Password Hacker\\task\passwords.txt", "r") as passfile:
        
        for word in passfile.readlines():
            
                
                if not word.strip("\n").isdigit():
                    for var in itertools.product(*([letter.lower(), letter.upper()]for letter in word.strip("\n"))):
                        yield ("".join(var))
                else:
                    yield (word.strip("\n"))


g = password_generator()


gg = count_pwd()

print(gg)

try: 
    for i in range(gg):
        print(g.__next__()) #prints same result everytime...
        
except:
    print('empty')

anyway you get unwanted duplicated is your password.txt file

contains pwds like 22B or 1a3

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1