MacPython Logo from __future__ import *

buy music albums Silver Apples buy mp3 albums Tarrus Riley buy tracks mp3 Kravits buy Reaper albums mp3 buy Kravits albums music buy music Evita CD online albums mp3 Silver Apples download Madonna CD music buy tracks music Kravits download music albums Silver Apples

2003-11-08

Yet Another Line Ending Conversion Program

Filed under: python — bob @ 6:52 pm

This is what I've been using.. it's really simple, and should convert Win32, Mac, or Unix line endings to whatever your native line ending is. A possible future optimization would be to use temp files instead, but they're annoying.

#!/usr/bin/env python
import sys, os, shutil
from cStringIO import StringIO

def tonative(fname):
    buff = StringIO()
    infile = file(fname, 'rU')
    infile.readline()
    infile.seek(0)
    if infile.newlines == os.linesep:
        return
    for line in infile:
        buff.write(line.strip() + os.linesep)
    infile.close()
    outfile = file(fname, 'wb')
    buff.seek(0)
    shutil.copyfileobj(buff, outfile)
    outfile.close()

if __name__ == '__main__':
    for fname in sys.argv[1:]:
        tonative(fname)

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Protected by WP-Hashcash.

Powered by WordPress