0

I'm trying to code a nested for loop where the ranges are defined by two input files, the content of the files is not really relevant for this problem. The files are passed to the program as arguments.

For each line of file A(which as an example contains numbers) the for loop should iterate through each of the lines in file B (which as an example contains letters).

Therefore the expected results is 1abc2abc3abc as output.

My current issue is that I don't quite grasp how the point at which the files are opened affects the loops.

Additionally I'm not quite sure if it would be more beneficial to open the files initially and then write the contents of both files line by line to a list to avoid any file's closing interfering with the program.

I've varied the locations of when file opening happens, which actually results in the program working as expected. Though as I said initially I'm not sure if this is considered the right way.

import sys

numbers = open(sys.argv[1], "r")
#letters = open(sys.argv[2], "r")

for line_num in numbers:

    print(line_num, end="")
    letters = open(sys.argv[2], "r")

    for line_let in letters:
        print(line_let, end="")

The code as represented currently works - I'm now opening the file containing letters within the first for loop. If I instead remove it from the for loop and uncomment, therefore open the file before the loop, I exactly get one iteration, i.e 1abc23 instead of 1abc 2abc 3abc,

sevasto
  • 3
  • 1
  • The reason you're seeing the result `1abc23` is that `open` returns an iterator over the lines of the file. Once you loop through the lines once, the iterator is exhausted. Subsequent times through the outer `for` loop, the inner `for` loop is skipped because `line_let` raises a `StopIteration` the first time the inner loop is tried. – Kyle Parsons Aug 05 '19 at 12:52
  • @KyleParsons that was my initial idea but I couldn't find any documentation on it. I solved the issue now by writing the file contents to a list and then working with the lists. – sevasto Aug 05 '19 at 13:01

2 Answers2

0

Typically with opening files and reading the stream from them you want to do a while loop until it reaches the end of the file. If the files are passed in as arguments I'm not sure having a loop reading the first file then an inner loop reading the second file makes sense. If you need to combine the data it probably makes more sense to read them separately then combine them. The only reason you'd be getting "1abc23" instead of 1abc2abc3abc" would be because in the letters file "abc" is in one line since it reads line by line. So if you want "1abc2abc3abc" then like I said earlier I suggest reading out letters and store that into a variable then loop through the numbers and append the letters at the end of each loop.

Gooby
  • 621
  • 2
  • 11
  • 32
  • Hi, cheers for your response. The files are currently identical, e.g. each letter/number is in a separate line. I afterwards need to do some processing on the pairs created by the loop but need to create the logic of creating the pairs first. It seems to me that when opening the file before the loops it gets closed after running the first iteration, is this a possible explanation? I think I'll try loading the files into lists and then perform the loops on the list, this would allow me to only read files once and close them directly after the lists have been created. – sevasto Aug 05 '19 at 08:27
  • Possible explanation but unlikely is that you can only have X amount of files open at one time, but I doubt it's 1 unless you're running on a really really old computer. I'm not super duper familiar with Python's library but it is possible that open() function uses the same stream which means by opening another file after you open the first it automatically closes the last one and opens the new one. If you like my answer give it the up arrow please! – Gooby Aug 06 '19 at 05:53
  • This might be useful? https://stackoverflow.com/questions/4617034/how-can-i-open-multiple-files-using-with-open-in-python Not 100% related cause you want to open them in a specific sequence where as this is both at the same time. – Gooby Aug 06 '19 at 05:55
-1

The main issue with your code is that you open the file and never close it, that is a bad practice and could lead to errors further on. You should've used the with open method. You should do as you said, store the info from the files in lists or a dictionary, and then just parse it.