Search and Replace With Python

Today I had a large (re: 4GB) file that I needed to do some search and replace on, and none of my text editors including my beloved Textmate could even open the file. So I turned to the most trusty of tools in my tool chest which is python!

f = open(infile, "r")
n = open(outfile, "w")
for line in f:
    output = line.replace(oldtext, newtext)
    n.write(output)

In just a couple of minutes I had a new file with the changes I needed. Its pretty basic stuff, but non-programmers (especially those that don't use languages like python) might not think of trying.

Subscribe to The Slakinski Log

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe