简体   繁体   中英

python nested loops with contents from two different files

I am trying to write a script to password spray user credentials. It take a password from pass_list file and then tries all the usernames from user_list. Then it will take the next password and try all the users with it. However, this loop doesn't work the way I want it.

user_list = open('user_list.txt')
pass_list = open('pass_list.txt')

for pass_word in pass_list:
    print(pass_word)

    for user_name in user_list:
        print(user_name)

The problem is it prints the usernames and passwords only once. It is supposed to print all usernames for a single password. Then for the second password it is supposed to iterate over the users again.

Files can only be read once. Use itertools.product :

with open('user_list.txt') as users:
    with open('pass_list.txt') as passwords:
        for username, password in itertools.product(users, passwords):
            print(username, password)

There are a few problems in your code.

  1. It should be pass_list=open('pass_list.txt','r') #=== Open file to read.
  2. It should be print(pass_list.read()) #=== Read the file

You can use print(pass_list.readlines()) which will create a list and add all the lines in the file to the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM