import string #open the input file infile = open("loops.py", "r") #open the output file outfile = open("loops2.py", "w") #read each line for line in infile: #print line #use an if statement to determine if an i is present on the line if 'i ' in line: #line2 = string.replace(line, "i ", "x ") #replace i with x line = line.replace("i ", "x ") else: line = line #store line in outputfile outfile.write(line) infile.close() outfile.close()