[Pythonmac-SIG] [HELP!] PC Line Endings? Batch file replacement

Richard Gordon richard@richardgordon.net
Wed, 20 Feb 2002 22:28:14 -0500


--============_-1197860393==_ma============
Content-Type: text/plain; charset="us-ascii" ; format="flowed"

At 12:25 PM +1100 2/21/02, Adam Eijdenberg wrote:
>Ben,
>
>A while back I got sick of this problem too, so I wrote a some 
>simple C code to accomplish it. Since it is short I'll place it 
>below:

Maybe I misunderstood the question, but it looks like you guys are 
working too hard. I haven't had much reason to use it lately, but the 
stuff below is pretty fast and recursively converts whatever is in a 
tree from unix or dos to mac (juggle the commented lines if you want 
to limit it to python files or whatever):

------
#!/usr/bin/env python

import sys, os, macfs
from time import time

start = time()
fcount = 0

def strip(f):
	global fcount
	infh = open(f, 'rb').read()
	outfh = open(f, 'wb')
	if infh.find('\r\n') != -1:
		outfh.write(infh.replace('\n',''))
	elif infh.find('\n') != -1:
		outfh.write(infh.replace('\n','\r'))
	else: pass
	outfh.close()
	fcount = fcount + 1
	return
def run(d):
	names = os.listdir(d)
	for name in names:
		f = d + ':' + name
		if os.path.isfile(f): strip(f)
##			if os.path.splitext(f)[1]=='.py':
##				strip(f)
		else: run(f)

if not sys.argv[1:]:
	fss, ok = macfs.GetDirectory('Folder to search:')
	if not ok:
		sys.exit(0)
	start = time()
	run(fss.as_pathname())
else:
	fss = sys.argv[1:]
	start = time()
	for arg in fss:
		run(arg)

stop = time()
elapsed = stop-start
print "Changed %d files in %d seconds" % (fcount, elapsed)

#eof


Richard Gordon
--------------------
Gordon Design
Web Design/Database Development
http://www.richardgordon.net
--============_-1197860393==_ma============
Content-Type: text/enriched; charset="us-ascii"

At 12:25 PM +1100 2/21/02, Adam Eijdenberg wrote:

<excerpt>Ben,


A while back I got sick of this problem too, so I wrote a some simple C
code to accomplish it. Since it is short I'll place it below:

</excerpt>

Maybe I misunderstood the question, but it looks like you guys are
working too hard. I haven't had much reason to use it lately, but the
stuff below is pretty fast and recursively converts whatever is in a
tree from unix or dos to mac (juggle the commented lines if you want to
limit it to python files or whatever):


------

<fontfamily><param>Monaco</param><smaller>#!/usr/bin/env python


import sys, os, macfs

from time import time


start = time()

fcount = 0


def strip(f):

	global fcount

	infh = open(f, 'rb').read()

	outfh = open(f, 'wb')

	if infh.find('\r\n') != -1:

		outfh.write(infh.replace('\n',''))

	elif infh.find('\n') != -1:

		outfh.write(infh.replace('\n','\r'))

	else: pass	

	outfh.close()

	fcount = fcount + 1

	return

def run(d):

	names = os.listdir(d)

	for name in names:

		f = d + ':' + name

		if os.path.isfile(f): strip(f)

##			if os.path.splitext(f)[1]=='.py':

##				strip(f)

		else: run(f)	


if not sys.argv[1:]:

	fss, ok = macfs.GetDirectory('Folder to search:')

	if not ok:

		sys.exit(0)

	start = time()

	run(fss.as_pathname())

else:

	fss = sys.argv[1:]

	start = time()

	for arg in fss:

		run(arg)


stop = time()

elapsed = stop-start

print "Changed %d files in %d seconds" % (fcount, elapsed)


#eof 
</smaller></fontfamily>



Richard Gordon

--------------------

Gordon Design

Web Design/Database Development

http://www.richardgordon.net

--============_-1197860393==_ma============--