I have been getting an error where a '.txt' file that I have opened will not be detected by multiple loops.
hand = open('filter_test.txt') # Open 1
# Loop 1
for line in hand:
line = line.rstrip()
if re.search(r'^X.*:', line):
print(line)
# I can still see 'hand' here.
print(hand)
# If 'Open 2' is not there, the 'Loop 2' will not pick up 'filter_test.txt'
hand = open('filter_test.txt') # Open 2
#Loop 2
for line in hand:
# If 'Open 2' is not present, line will not print anything here.
print(line)
line = line.rstrip()
if re.search(r'^X-\S+:', line):
print(line)
I can still see 'hand' between 'Loop 1' and 'Loop 2', but if I do not have the 'Open 2', 'Loop 2' will not pick it up.
Any idea why this is? It seems silly having to reopen a file for each loop I want to run
Cheers