I'm trying to write a script in Python that will go through a folder and subfolders, enter each file and read its content, and replace some strings in the file with another string.
What I managed to do so far is to go through my folder, open up the files and read the content, and also replace strings, but I want to write this change to the file and I can't seem to find how to do it in my code.
import os
rootDir = '.'
for root, dirs, files in os.walk (rootDir):
for fileID in files:
content = open(rootDir + '\\' + fileID, 'r').read()
replacedContent = content.replace ('line', 'word')
print replacedContent
My final purpose is to create a script that will take the values of the words to find and words to replace from an Excel sheet (Instead of 'line' and 'word'), perform the changes to the file and then save it.
EDIT
I don't want to close the file and re-write the content into it, but i'm searching for a better way to insert the text to the file itself, becuase my final purpose will be to perform a few changes in the text and not only one word, and opening and closing the file will take more time and i'm afraid will cause memory issues.