the following code opens a file searches for a word or phrase, then opens the file in an array, it then adds two new objects after the word or phrase and then re-writes it to the file, the with statments do not work, when compiled it produces a syntax error saying the file = open(...) the '=' is not valid but it is the assignment operator. help?
def edit(file_name,search_parameters,added_data,second_data):
with(file = open(file_name,'r')):
lines = list(file)
file.close()
linenum = (num for (num,line) in enumerate(lines) if search_parameters in line).next()
lines[linenum+1] = added_data
lines[linenum+1] = second_data
with (file2 = open(file_name,"w")):
file2.writelines(line + '\n' for line in lines)
file2.close()