0

I have a users.txt and password.txt and i need to iterate in both files

Example of users.txt:

  • root
  • admin
  • oracle
  • users

Example of password.txt

  • toor
  • rooot
  • administrator
  • password

In my loop just print the first user "root" and all of the password but dont print the others users, this is my code:

fu = open("/home/dskato/diccionarios/user.txt", "r")
fp = open("/home/dskato/diccionarios/pasword.txt", "r")

for user in fu.readlines():
  for password in fp.readlines():
    print "Username: "+user+" Password: "+password

This is the output but don't iterate over the others users What it's wrong?

Username: root Password: 123456

Username: root Password: 12345678

Username: root Password: 12345678

Username: root Password: 1234

Username: root Password: pass

Username: root Password: password

And i need this output example:

  • root: 123456
  • root:password
  • root:toor
  • admin:123456
  • admin:password
  • admin:toor
MADMVX
  • 354
  • 1
  • 4
  • 18

2 Answers2

3

You are iterating over all the passwords with the inner loop on the first iteration of the outer loop. On the second iteration of the outer loop, there are no more passwords to iterate (fp's current position is at the end of the file).

You could fp.seek(0) to reset fp after each user iteration:

for user in fu:
    for password in fp:
        print "Username: " + user.rstrip('\n') + " Password: " + password.rstrip('\n')
    fp.seek(0)

Or you could read the passwords once into a list:

passwords = list(fp)

for user in fu:
    for password in passwords:
        print "Username: " + user.rstrip('\n') + " Password: " + password.rstrip('\n')

There's a tradeoff between file access and memory usage here.

Galen
  • 1,307
  • 8
  • 15
0

You have a nested for loop, which reads passwords (in the internal loop), for each users (in the external loop).

What happened now, your external loop read first user (root), and the internal read all password, and print them together with root.

When your external loops tries to process second user, your password file is already reaching the end, nothing is printed.

You need to read lines of these files separately, store it in an array and print later.

Better yet, just use paste : https://unix.stackexchange.com/questions/234208/combining-several-files-into-a-single-csv

wibisono
  • 61
  • 4