[newbie] Read file, and append?

Gilles Ganault nospam at nospam.com
Thu Dec 27 03:27:14 EST 2007


On Wed, 26 Dec 2007 14:33:15 -0800, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:
>Works for me, as long as one uses a seek() call when switching
>between read and write...

Thanks, Dennis. Worked :-) I just changed the access mode from binary
to text so that Python handles the EOL character correctly, ie. CRLF
for Windows instead of LF for *nix.

For those interested, here's some working code in Windows +
ActivePython:

==========
import glob
    
#activate.tmpl contains the stuff I want to append to each file
f = open("activate.tmpl", "r")
template = "\n\n" + f.read()
f.close()

for frm in glob.glob('*.frm'):
	f = open(frm, "r+")
	content = f.read()
	if 'Form_Activate' not in content:
		f.seek(0,2)
		f.write(template)
	f.close()
==========



More information about the Python-list mailing list