I am learning to code in Python. Now I am experimenting with a file comparison program from here.
My code is:
#!/usr/bin/python3
def main():
fhand1 = open('mbox.txt')
print('file handle for mbox is {}'.format(fhand1))
count = 0
for l1 in fhand1:
count = count + 1
l1 = l1.rstrip() # Skip 'uninteresting lines'
if l1.startswith('From:'):
print('{}'.format(l1))
print('Numer of lines: {}'.format(count))
fhand2 = open('mbox-short.txt')
#inp = fhand2.read(), when here for loop does not work
#for l2 in fhand2:
#if l2.startswith('From:'):
#print('{}'.format(l2))
inp = fhand2.read()#if for loop is active then this doesnot work
print('Total characters in mbox-short: {}'.format(len(inp)))
print('First 20 characters on mbox-short: {}'.format(inp[:56]))
if __name__ == "__main__": main()
My question is for 'mbox-short.txt'. When I put inp = fhand2.read()
before the for l2 in fhand2: {}
the for loop does not run. When I change the sequence, the read()
operation does not work.
Can someone please explain this?
Btw, I am using JetBrains PyCharm Community Ed 4 IDE.
Thank you in advance.