Rajiv Sharma Rajiv Sharma - 10 months ago 45
Python Question

python search and replace strings in 2GB text file size

I want to search and replace some file paths in text files.
on average the my file size is above 2 GB.

for example:
i have a folder in my D drive "D:\LargeFilesFolder".
and all files exist in the folder.


In all text files i want to search for
and replace with

What is the best optimized python way to read and edit big text files in python..?


Iterate over the lines and write a new file. Then move the new file over the old file.

with open(outputfilename, "w") as outputfile:
    with open(inputfilename, "r") as inputfile:
        for line in inputfile:
            # replace in line and write to outputfile

shutil.move(outputfilename, inputfilename)