if inplace is set as 1, then print will flush back the output to input file
def file_insert( fname, linenos = [], strings = [] ):
lineno = 0 # record the line no in the file
i = 0 # record the no. in linenos
for line in fileinput.input( fname, inplace = 1 ):
lineno += 1
line = line.strip() #for print add a "/n" to each line
# no finish each element in linenos
if i < len( linenos ) and linenos[i] == lineno:
# print "", for index not in strings
if i >= len( strings ):
print "/n", line
else:
print strings[i]
print line
i += 1
else:
print line
file_insert( "a.txt", [1,4,5], ["insert1", insert3"])