Problem Commenting within Filehandle Iteration

W

Wijaya Edward

Hi all,

I have the following code:


import sys
import re

ham_count = 0
spam_count = 0
myfile = open('full/index')
for line in myfile.readlines():
p = re.compile('ham')
m = p.match(line)
if m:
print line,
else:
#print 'SPAM -- %s' % line
myfile.close()

Sometime while developing/debugging the code we usually
put in such situation. Where expression under "else"
is not yet supplied, yet we would like see the printout of the
previous "if" condition.

Notice that I wanted to comment out the #print line there.
However I found problem with myfile.close(), with identation error.
This error doesn't occur when commenting (#) is not in use.

Why so? Is there away to do the commenting in correct way
under this circumstances?

--
Edward WIJAYA
SINGAPORE



------------ Institute For Infocomm Research - Disclaimer -------------
This email is confidential and may be privileged. If you are not the intended recipient, please delete it and notify us immediately. Please do not copy or use it for any purpose, or disclose its contents to any other person. Thank you.
--------------------------------------------------------
 
M

Marc 'BlackJack' Rintsch

Wijaya Edward said:
if m:
print line,
else:
#print 'SPAM -- %s' % line
myfile.close()

[…]

Notice that I wanted to comment out the #print line there.
However I found problem with myfile.close(), with identation error.
This error doesn't occur when commenting (#) is not in use.

Why so? Is there away to do the commenting in correct way
under this circumstances?

There has to be code in the ``else`` block. So either comment out the
``else`` line too, or insert a ``pass`` statement.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top