<div dir="ltr">On Fri, Apr 12, 2013 at 4:01 PM, Kurtis Mullins <span dir="ltr"><<a href="mailto:kurtis.mullins@gmail.com" target="_blank">kurtis.mullins@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="im">On Fri, Apr 12, 2013 at 10:31 AM, Eric Floehr <span dir="ltr"><<a href="mailto:eric@intellovations.com" target="_blank">eric@intellovations.com</a>></span> wrote:<br>

</div><div class="gmail_extra"><div class="gmail_quote"><div class="im">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">


<div><br></div><div><br></div><div>And if you are using Python 2.5 or higher, this is "best", as it closes the file always, even on an exception:</div>

<div><br></div><div>with open('foo', 'rU') as f:</div><div>    for line in f:</div><div>        # Do whatever with line or pass</div></div></div></div></blockquote><div><br></div></div><div>That's actually what I use as well; however, I still have to check for blank lines. I assumed that's what jep was alluding to.</div>

</div></div></div></blockquote><div><br></div><div style>Not exactly, because a "blank line" returned from readline() (or the iterator) signals end of file. A true "blank line" in a file will be returned as '\n' since newlines aren't stripped when readline() reads the line.</div>

<div style><br></div><div style>In other words '' (empty string) indicates EOF, while '\n' indicates an empty line.</div><div style><br></div><div style>The iterator version handles it... when it gets an EOF it ends the iteration, but if you readline() yourself, you have to check, otherwise you'll forever get empty strings once the file is read.</div>

<div style><br></div><div style><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>For example (out of a script I recently wrote):<br>

</div><div><br></div><div><div>with open(negation_lexicon_file_name, 'rb') as negation_file:</div><div>    for line in negation_file:</div>
<div>        phrase = line.strip().lower()</div><div>        if phrase:</div><div>            negation_lexicon['lexemes'].append(phrase)</div></div></div></div></div></blockquote><div><br></div><div style>Seems like a great way to do that.</div>

<div style><br></div><div style>-Eric</div><div style><br></div></div></div></div>