"\n" in ASCII-file

Gerhard Häring gh at ghaering.de
Fri Jul 18 13:26:59 CEST 2003

Martin P wrote:
> Hello,
> for a short exercise-program I have to read the lines in an ASCII-file.
> But every line has a new-line-feed ("\n") at the end of the line. (The
> ASCII-file was made with Windows Notepad).
> Is this only because I used Windows (and Notepad)?


> And... is there any special method to read lines from a file without "\n"?

Most of the time, I want to strip any trailing whitespace and do 
something like:

f = file("/path/to/my/file")
for line in f:
     line = line.strip()
     # ...

Unlike Karl's method, this scales to larger files without needing to 
read the whole file into memory.

Files are iterable since Python 2.2.

-- Gerhard

More information about the Python-list mailing list