[newbie] Read file, and append?

Gilles Ganault nospam at nospam.com
Tue Dec 25 03:44:18 EST 2007


Hello

I'd just like to open each file in a directory with a given extension,
read it to search for a pattern, and, if not found, append data to
this file. The following doesn't work:

======
import glob,re
    
f = open("activate.tmpl", "r") 
template = f.read()
template = "\r\n" + template
f.close()

for file in glob.glob('*.frm'):
	#BAD f = open(file, "r+") 
	#f = open(file, "r") 
	#f = open(file, "a") 

	f = open(file, "rw") 
	if not re.search('Form_Activate',f.read(), re.I):
		#print "Not in " + file

		#IOError: [Errno 0] Error
		f.write(template)

		f.close()
======

What am I doing wrong?

Thank you.



More information about the Python-list mailing list