By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

Problem Commenting within Filehandle Iteration

P: n/a

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.
--------------------------------------------------------
Oct 26 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In <ma***************************************@python. org>, Wijaya Edward
wrote:
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
Oct 26 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.