Have you made a diff of your files before and after the modification to make sure that your script works as intended ?
I'm not a Python expert, but I see you open your file once in read mode and a second time in write mode without closing it first. In case the Windows implementation of your Python interpreter opens the file with an exclusive lock, your second open() call might fail.