'why do my nested loops don't work when I read CSV files in python?

I've got two CSV files, with one row in each file, I've coded a nested loop but not all the values from the second CSV file execute when the values of the first file iterate.

these are the words from the first file: 1a1dc91c907325c69271ddf0c944bc72

482c811da5d5b4bc6d497ffa98491e38

5d9d4b99816eea5cd36b5afbfec45d84

33a6d4da4600ce15a4203e525c09c745

these are the words from the second file: pass,

password123 password123!@; , thisisapassword:0

the second file iterates but only one value from the first file executes.

import csv

with open('hash.txt') as csvfile , open('wordlist.txt') as wrd:

for word in csvfile:
  for words in wrd:
    print(word, words)


Solution 1:[1]

You only open('wordlist.txt') once, but you try to iterate through the whole thing more than once. After the first pass through, the iterator is exhausted.

Rather than re-reading wordlist.txt once per line of hash.txt, I'd suggest reading it into a list once so you can iterate over it in memory as you read hash.txt:

import csv

with open('wordlist.txt') as f:
    wordlist = f.readlines()

with open('hash.txt') as f:
    for word in f:
        for w in wordlist:
            print(word, w)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Samwise