Python - Brute Force reading TXT file and generating an output with six password variations

Asked

Viewed 79 times

-2

The function of this code is to read a txt file (let’s imagine that in this txt have two words: Beautiful house and, in the other line, dirty car), after reading, is created an output txt (Result.txt), with the variations of the words contained in the first txt(Ex: Beautiful House, BEAUTIFUL HOUSE, BEAUTIFUL HOUSE, CASABONITA, etc.). I can generate these results, but when I ask to read a txt that contains ten thousand more words, during the execution process it hangs and closes, thus generating the output of the files that managed to make the change until locking.

Follow the code below.

entrada = input('Caminho do txt p/ leitura >> ')

print('Arquivo carregado', '\n')

arq = open(entrada, 'r')
print('>> Processando', '\n')

saida = open('Resultado.txt', 'w')

for linha in arq:
    if linha.strip().strip("\n"):
        saida.writelines(linha.upper().strip() + '\n')
        saida.writelines(linha.lower().title().strip() + '\n')
        saida.writelines(linha.lower().strip() + '\n')
        saida.writelines(linha.upper().replace(" ", "").strip() + '\n')
        saida.writelines(linha.lower().title().replace(" ", "").strip() + '\n')
        saida.writelines(linha.lower().replace(" ", "").strip() + '\n')

arq.close()
saida.close()

print('>> Processo Concluido')
  • in this case, use ". write" instead of ". writelines". The "writelines" method of files is done to write several strings that are within a sequence - for example, a list of file lines. Since a string is a sequence of strings of 1 character, by coincidence it works in this case - but it consumes a lot more resources. (shouldn’t brake, but this may be the cause)

  • 1

    All box variations a string is the factorial of its length for example the phrase mOça boNitA generates 3628800 box combinations or consumes 3.5 MB of memory. If you do this for 10 thousand words you must be pressing the RAM.

  • Well remembered. Now imagine the memory consumption and space to be used in HD if we tried to produce all possible combinations of uppercase and lowercase letters of said words, combined with hardware configuration restrictions?

1 answer

1


You can load row by row instead of loading the entire file and apply the treatment.

note that in the snipped below it is not necessary to close the input file why the method with already manages it automatically.

def do_tratamento(line) -> list:
    #Code para cada formato

saida = open('Resultado.txt', 'w')
with open(entrada,'r') as fp:
    line = fp.readline()
    while line:
        saida.writelines(do_tratemento(line))

saida.close()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.