[Tutor] Blank line added when reading in from file????

Phelim Kelly phelimkelly@hotmail.com
Wed, 05 Dec 2001 09:54:17 +0000


<html><div style='background-color:'><DIV>Hello,</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I have a small probelm, hope you can help. I'm reading text from an external file into a python program as follows:</DIV>
<DIV>&nbsp;</DIV>
<DIV>filename&nbsp; = raw_input("Enter the filename to read from: ");</DIV>
<DIV>p=0<BR>in_file = open(filename,"r")<BR>while p &lt; 4:<BR>&nbsp;&nbsp; text = in_file.readline()<BR>&nbsp;&nbsp; list[p]=text&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #list is an array which stores the content of each line read in.<BR>&nbsp;&nbsp; p = p + 1<BR>in_file.close()<BR><BR></DIV>
<DIV>The problem I have is that the text that is stored in variable 'text' isn't simply the contents of one line of the file, another blank line is appended onto the end, which causes problems for the rest of the program, so instead of this,</DIV>
<DIV>&nbsp;</DIV>
<DIV>---------------------</DIV>
<DIV>line read in from file</DIV>
<DIV>--------------------</DIV>
<DIV>&nbsp;</DIV>
<DIV>I get this,</DIV>
<DIV>&nbsp;</DIV>
<DIV>-------------------</DIV>
<DIV>line read in from file</DIV>
<DIV>&nbsp;</DIV>
<DIV>-------------------</DIV>
<DIV>&nbsp;</DIV>
<DIV>Can anyone tell me the command used to get rid of the extra blank line added on?</DIV>
<DIV>Thanks in advance,</DIV>
<DIV>Phelim.</DIV></div><br clear=all><hr>Get your FREE download of MSN Explorer at <a href='http://go.msn.com/bql/hmtag_itl_EN.asp'>http://explorer.msn.com</a><br></html>