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,