python replace part in large file

for file in files:
    fname = os.path.join(dir, file)
    inFile = codecs.open(fname, "r", "utf-8")
    outFile = codecs.open(fname + ".new", "w", "utf-8")
    for line in inFile:
        newline = do_replacements_on(line)
        outFile.write(newline)
    inFile.close()
    outFile.close()
    os.rename(fname + ".new", fname)

Here is what the above code is Doing:
1. It’s looking for all the files in the directory.
2. It’s opening each file, reading it line by line, and writing the new line to a new file.
3. It’s closing the old file and renaming the new file to the old file’s name.