Moving from perl to python: questions

Bernhard Herzog herzog at
Wed Feb 16 16:59:32 CET 2000

Gaetan Corneau <Gaetan_Corneau at> writes:

> You can simplify "chomp", too:

Well, there are at two bugs in this implementation:

> # I didn't try it, but it should work :)
> def chomp(lines):
>    "remove the end of line markers from array"
>    import os
>    nl_len = len(os.linesep)   # length of os dependent line seperator

os.linesep is irrelevant here. The line separator will always be '\n'.
The underlying C-library takes care of translating whatever platform
specific convention is used into newlines if you opened the file in text
mode (the default). 

If the lines were read from a file in binary mode you might get problems
with readline (or readlines) because they only recognise '\n' as the
line separator and therefore won't work correctly with e.g.

>    for i in range(0, len(lines)):
>     lines[i] = lines[i][:-nl_len]

The last line may not end in a newline.

Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at  |

More information about the Python-list mailing list