-2

I have 2 files, passwd and dictionary. The passwd is a test file with one word, while the dictionary has a list of a few lines of words. My program so far reads and compares only the first line of the dictionary file. For example. My dictionary file contain (egg, fish, red, blue). My passwd file contains only (egg).

The program runs just fine, but once I switch the word egg in the dictionary file to lets say last in the list, the program wont read it and wont pull up results.

My code is below.

#!/usr/bin/passwd

import crypt

def testPass(line):
    e =  crypt.crypt(line,"HX")
    print e

def main():
    dictionary = open('dictionary', 'r')
    password = open('passwd', 'r')
    for line in dictionary:
        for line2 in password:
            if line == line2:
                testPass(line2)
    dictionary.close()
    password.close()

main()
Onlytito
  • 167
  • 1
  • 2
  • 17

2 Answers2

1

If you do

for line in file_obj:
  ....

you are implicitly using the readline method of the file, advancing the file pointer with each call. This means that after the inner loop is done for the first time, it will no longer be executed, because there are no more lines to read.

One possible solution is to keep one -- preferably the smaller -- file in memory using readlines. This way, you can iterate over it for each line you read from the other file.

file_as_list = file_obj.readlines()
for line in file_obj_2:
  for line in file_as_list:
    ..
Jasper
  • 3,939
  • 1
  • 18
  • 35
0

Once your inner loop runs once, it will have reached the end of the password file. When the outer loop hits its second iteration, there's nothing left to read in the password file because you haven't move the file pointer back to the start of the file.

There are many solutions to the problem. You can use seek to move the file pointer back to the start. Or, you can read the whole password file once and save the data in a list. Or, you can reopen the file on every iteration of the outer loop. The choice of which is best depends on the nature of the data (how many lines there are, are they on a slow network share or fast local disk?) and what your performance requirements are.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685