[Tutor] Getting rid of the newline in file

Alan G alan.gauld at freenet.co.uk
Sat Sep 17 11:08:09 CEST 2005

> f = file("foo", 'w')

Using 'w' will destroy the contents of the file, 
I assume you are really using 'r'?

> fileContents = f.readlines()

Each line will be terminated by a newline, you 
can use rstrip() to remove it:

> print fileContents[0].rstrip() + "Hi!"


There is more on this topic in the file handling 
topic of my tutorial.

Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list