[Tutor] Blank line added after reading a line from a file

Andrei Kulakov ak@silmarill.org
Wed, 05 Dec 2001 07:40:15 -0500


On Wed, Dec 05, 2001 at 12:29:55PM +0000, Kelly, Phelim wrote:
> (I'll try this again!)
> 
> Hello, 
> I have a small probelm, hope you can help. I'm reading text from an external
> file into a python program as follows: 
> 
> ---------------------
> filename = raw_input("Enter the filename to read from: "); 
> p=0
> in_file = open(filename,"r")
> while p < 4:
>    text = in_file.readline()
>    list[p]=text #list is an array which stores the content of each line read
> 
>                 #in.
>    p = p + 1
> in_file.close()

This would be easier done as:

in_file = open(filename)
text = inf_file.readlines()
lines_1_4 = text[:4]

That is, assuming file will comfortably fit in memory, which is the case
with most text files.

> ---------------------------
> 
> 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, 
> --------------------- 
> line read in from file 
> -------------------- 
> 
> I get this, 
> 
> ------------------- 
> line read in from file 
> 
> ------------------- 
> Can anyone tell me the command used to get rid of the extra blank line added
> on? 

I think this has to do with how you print them out... how do you do
that?

> Thanks in advance, 
> Phelim.
> 
> This e-mail and any attachment is for authorised use by the intended recipient(s) only.  It may contain proprietary material, confidential information and/or be subject to legal privilege.  It should not be copied, disclosed to, retained or used by, any other party.  If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.  Thank you.
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org