Friday, August 24, 2018

list - Read TXT file line by line - Python

How can I tell python to read a txt list line by line?

I'm using .readlines() which doesn't seem to be working.



import itertools
import string
def guess_password(real):
inFile = open('test.txt', 'r')
chars = inFile.readlines()
attempts = 0
for password_length in range(1, 9):
for guess in itertools.product(chars, repeat=password_length):

attempts += 1
guess = ''.join(guess)
if guess == real:
return input('password is {}. found in {} guesses.'.format(guess, attempts))
print(guess, attempts)

print(guess_password(input("Enter password")))


The test.txt file looks like:




1:password1
2:password2
3:password3
4:password4


currently the program only works with the last password on the list (password4)
if any other password is entered it will run past all the passwords on the list and return "none".




So I assume I should be telling python to test each line one at a time?



PS. the "return input()" is an input so that the dialog box doesn't close automatically, there's nothing to be inputted.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...