You can also read all of the file into memory, process it once to search for the string, and then process it a second time to write it to a file if necessary.
data = open(filename, "r").readlines()
found = 0
for rec in data:
if ("sa001:" in rec) or ("SA001:") in rec:
print "SA001 found"
found =1
## file only changes if string is not found
if not found:
fp = open(filename+".2", "w")
fp.write( "SA001:\n") ## add this line
for rec in data: ## original data
fp.write(rec)
fp.close()
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
Offline 2,305 posts
since Dec 2006