#!/usr/bin/env python """\ convert dos linefeeds (crlf) to unix (lf) usage: dos2unix.py """ import sys import os if len(sys.argv[1:]) != 1: sys.exit(__doc__) content = '' outsize = 0 fpath = os.path.dirname(os.path.abspath(sys.argv[1])) dfile = os.path.join(fpath, sys.argv[1]+"_chg") with open(sys.argv[1], 'rb') as infile: content = infile.read() with open(dfile, 'wb') as output: for line in content.splitlines(): outsize += len(line) + 1 output.write(line + '\n') if ((len(content)-outsize) == 0): os.remove(dfile) else: os.remove(sys.argv[1]) os.rename(dfile, sys.argv[1]) print("Done. Saved %s bytes." % (len(content)-outsize))