I am completely new to Python and this is my first script to replace word.
my file test.c
contains following two lines
printf("\nReboot not supported. Exiting instead.\n");
fprintf(stderr, "FATAL: operation not supported!\n");
Now I want to replace printf
and fprintf
by //printf
and //fprintf
respectively.
Here is what I have tried
infile = open('path\to\input\test.c')
outfile = open('path\to\output\test.c', 'w')
replacements = {'printf':'//printf', 'fprintf':'//fprintf'}
for line in infile:
for src, target in replacements.iteritems():
line = line.replace(src, target)
outfile.write(line)
infile.close()
outfile.close()
But using this I got
fprintf
to //f//printf
which is wrong.
For the solution have looked this answer but not able to fit it in my script.
Anyone have idea how can I fix it?