Rajiv Sharma Rajiv Sharma - 6 months ago 22
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.

"D:\LargeFilesFolder\large_file_v001.txt"
"D:\LargeFilesFolder\large_file_v002.txt"
"D:\LargeFilesFolder\large_file_v003.txt"


In all text files i want to search for
"X:\path\link\here"
and replace with
"Y:\here\link\path"


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

Answer

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)
Comments