4

Currently, this code is giving me all lines from searchfile once. But with my newbie understanding, it should print out all lines from searchfile for every infile line!

searchfile = open('.\sometext.txt', 'r')
infile = open('.\somefile', 'r')

for line1 in infile:
    for line2 in searchfile:
        print line2

searchfile.close()
infile.close()

I tried to use searchfile.readlines() to create a list to print all infile lines for all searchfile lines, but it still does not work. Does anyone have a clue?

Georgy
  • 12,464
  • 7
  • 65
  • 73
  • Duplicate: [Iterating on a file doesn't work the second time](https://stackoverflow.com/q/10255273/7851470) – Georgy Nov 28 '19 at 11:12

4 Answers4

6

I suppose searchfile is a file you opened earlier, e. g. searchfile = open('.\someotherfile', 'r').

In this case, your construction doesn't work, because a file is an iterable which can be iterated over only once and then it is exhausted.

You have two options here:

  1. Reopen the file on every outer loop fun
  2. Read the file's contents into a list an iterate over this list as often as you need to.

What happens in your code?

At the start of the nested for loops, both your files are open and can be read from.

Whenever the first inner loop run is over, searchfile is at its end. When the outer loop now comes to process its second entry, the inner loop is like an empty loop, as it just cannot produce more entries.

glglgl
  • 89,107
  • 13
  • 149
  • 217
5

The in and with keywords

You don't need two nested for loops! Instead use the more pythonic in keyword like so:

with open("./search_file.txt", mode="r") as search_file:
    lines_to_search = search_file.readlines()

with open("./file_to_search.txt", mode="r") as file_to_search:
    for line_number, line in enumerate(file_to_search, start=1):
        if line in lines_to_search:
            print(f"Match at line {line_number}: {line}")

Pro tip: Open your files using the with statement to automatically close them.

winklerrr
  • 13,026
  • 8
  • 71
  • 88
  • Thanks for the pro tip! I used the seek function and it solves the problem. You are creating a list and only one for loop, which seems to be more efficient! Thanks! – Kevin Winnik Nov 26 '19 at 10:14
5

You need to set searchfile current position to the beginning for every infile iteration. you can use seek function for this.

searchfile = open('.\sometext.txt', 'r')
infile = open('.\somefile', 'r')

for line1 in infile:
    searchfile.seek(0,0)
    for line2 in searchfile:
        print line2

searchfile.close()
infile.close()
Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44
-1

We need a bit more details about what are your objects in this code. But you probably would like to do:

    infile = open('.\somefile', 'r')

    for line1 in infile:
       for line2 in line1:
         print line2

    searchfile.close()
    infile.close()

If your infile is a list of lists - That are the cases where a nested for loop would make sense.

  • @Kevin Winnik stated, that two files exist. And that a line of `searchfile` is supposed to be printed for every line of `infile`. – LeoE Nov 26 '19 at 09:43