During the iteration over the text file and over the list I've found the unexpected behavior of for
loop.
Text file test.txt
consists of only two strings: 1) He said:
and 2) We said:
.
First for
+for
loop
file_ = open('c:\\Python27\\test.txt', 'r')
list_ = ['Alpha','Bravo','Charlie']
try:
for string in file_:
for element in list_:
print string, element
except StandardError:
print 'Ooops'
returns absolutely expectable result:
He said: Alpha
He said: Bravo
He said: Charlie
We said: Alpha
We said: Bravo
We said: Charlie
But if the for
order was changed to
file_ = open('c:\\Python27\\test.txt', 'r')
list_ = ['Alpha','Bravo','Charlie']
try:
for element in list_:
for string in file_:
print string, element
except StandardError:
print 'Ooops'
the result is totally different:
He said: Alpha
We said: Alpha
Looks like the first for
became uniterable. Why?