first post here. I just started to learn python and I can't explain this issue after having spent hours on it.
If I insert the line #A (see the code below), the for loop doesn't print anything. If instead I delete the line #A or insert handle = open(file, 'r') before the loop, the for loop works again. Why does this happen? Since handle was defined in try, why shuould I define it again after read?
PS: the #A line doesn't have any purpose. I am just trying to understand how python works. Shouldn't the for loop just ignore that line?
try:
file = input('Enter a file name: ')
handle = open(file, 'r')
except:
print('Invalid file')
quit()
openhandle = handle.read() **#A**
for line in handle:
if not 'From stephen.marquard@uct.ac.za Fri Jan 4 04:07:34 2008' in line:
continue
print((line.rstrip()).upper())
I do not understand the issue. The read and for loop predictably work in other codes I made. Thanks in advance!