Find and replace texts in all files from the text file input using Python in Notepad++
<p>I'm using Notepad ++ to do a find and replacement function. Currently I have a a huge numbers of text files. I need to do a replacement for different string in different file. I want do it in batch. For example.</p>

<p>I have a folder that has the huge number of text file. I have another text file that has the strings for find and replace in order</p>

<p>Text1 Text1-corrected</p>

<p>Text2 Text2-corrected</p>

<p>I have a small script that do this replacement only for the opened files in Notepad++. For achieving this I'm using <strong>python script</strong> in <strong>Notepad++</strong>. The code is as follows.</p>

<pre><code> with open('C:/replace.txt') as f:
 for l in f:
    s = l.split()
    editor.replace(s[0], s[1]) 
</code></pre>

<p>In simple words, the find and replace function should fetch the input from a file.</p>

<p>Thanks in advance. </p>

<pre><code>with open('replace.txt') as f:
    replacements = [tuple(line.split()) for line in f]
for filename in filenames:
    with open(filename, 'w') as f:
        contents = f.read()
        for old, new in replacements:
            contents = contents.replace(old, new)
        f.write(contents)
</code></pre>

<p>Read replacements into a list of tuples, then go through each file, and read the contents into memory, do the replacements, then write it back.  I think the files get overwritten properly, but you might want to double check.</p>

python replace find notepad++