perl chomp equivalent in python?

Fred L. Drake, Jr. fdrake at acm.org
Thu Feb 10 21:35:26 CET 2000


Gerrit Holl writes:
 > >>> import os
 > >>> lines = open("path-to-file", 'r').readlines()
 > >>> for line in lines:
 > ...     print line[:len(os.linesep)]
 > 
 > cross-platformness-rules-ly y'rs - gerrit

Gerrit,
  This won't work on DOS or Windows.  os.linesep is two characters,
but the line will only contain a '\n', since the file was opened in
text mode.  The original code was fine.  ;)


  -Fred

--
Fred L. Drake, Jr.	  <fdrake at acm.org>
Corporation for National Research Initiatives




More information about the Python-list mailing list