python replace part in large file

Solutions on MaxInterview for python replace part in large file by the best coders in the world

showing results for - "python replace part in large file"
Kaylen
12 Aug 2020
1for file in files:
2    fname = os.path.join(dir, file)
3    inFile = codecs.open(fname, "r", "utf-8")
4    outFile = codecs.open(fname + ".new", "w", "utf-8")
5    for line in inFile:
6        newline = do_replacements_on(line)
7        outFile.write(newline)
8    inFile.close()
9    outFile.close()
10    os.rename(fname + ".new", fname)
11