"\n" in ASCII-file
gh at ghaering.de
Fri Jul 18 13:26:59 CEST 2003
Martin P wrote:
> 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
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.
More information about the Python-list