crlf.py 603 Bytes
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2
#! /usr/local/bin/python

3 4 5 6
# Replace \r by \n -- useful after transferring files from the Mac...
# Run this on UNIX.
# Usage: crlf.py file ...

Guido van Rossum's avatar
Guido van Rossum committed
7 8 9 10 11 12 13
import sys
import os
import string

def main():
	args = sys.argv[1:]
	if not args:
14 15
		print 'usage:', sys.argv[0], 'file ...'
		sys.exit(2)
Guido van Rossum's avatar
Guido van Rossum committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29
	for file in args:
		print file, '...'
		data = open(file, 'r').read()
		lines = string.splitfields(data, '\r')
		newdata = string.joinfields(lines, '\n')
		if newdata != data:
			print 'rewriting...'
			os.rename(file, file + '~')
			open(file, 'w').write(newdata)
			print 'done.'
		else:
			print 'no change.'

main()