jConvert : convert line endings (dos to unix) in python

Download jconvert.zip

Synopsis:

jconvert.py


jconvert.py

Synopsis
import sys
import os
import os.path

def convert(f):
    try:
      infh = open(f, 'rb')
      inbuf = infh.read()
      infh.close()
    except IOError, msg:
      return
    os.rename(f, f + '~')
    outfh = open(f, 'wb')
    if inbuf.find('\x0D\x0A') != -1:
      outfh.write(inbuf.replace('\x0D',''))
    elif inbuf.find('\x0D') != -1:
      outfh.write(inbuf.replace('\x0D','\x0A'))
    else:
      outfh.write(inbuf)
    outfh.close()
    os.remove(f + '~')

convert(sys.argv[1])






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010