[Tutor] file eats first character, film at 11

Magnus Lyckå magnus@thinkware.se
Sat Jun 14 19:37:01 2003


At 12:41 2003-06-14 -0400, Kirk Bailey wrote:
>ok, I wrote a program to strip out the ms-dos CRLF charpair, replacing 
>with a single \n char. Works fine.

That seems like a rather longwinded way of doing (untested) ...

import sys
LINE_BREAK = '\n'

if len(sys.argv) == 1:
     # No file names on command line
     sys.argv.append(raw_input('Enter file name: '))

for fn in sys.argv[1:]:
     f = file(fn, 'r')
     data = f.read()
     f.close()

     f = file(fn, 'w')
     f.write(LINE_BREAK.join(data.splitlines())+LINE_BREAK)
     f.close()

Note that a difference between DOS and Unix text files beyond
the use of \r\n instead of just \n, is that Unix expects the
file to have a final \n after the last line. Otherwise this
line is often considered incomplete. Typically, Unix editors
will always add a \n in the end of the file, while for instance
notepad.exe won't unless yo end your file with an explicit
line break (Enter). Just replacing all \r\n with \n won't fix
that, but requires an extra check to see that the file ends
with \n.


--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language