Skip to content

Instantly share code, notes, and snippets.

@geeksunny
Created October 14, 2013 15:37
Show Gist options
  • Save geeksunny/6977638 to your computer and use it in GitHub Desktop.
Save geeksunny/6977638 to your computer and use it in GitHub Desktop.
A quick'n'dirty Python script to make find & replace operations on large text files fast and automated. Creates a duplicate file copy and leaves the original file unchanged.
#!/usr/bin/python
## Configuration ##
__target_file__ = "example.txt" # Relative path to the target file to process.
__find__ = "found" # Block of text to find.
__replace__ = "replaced" # Block of text to replace with.
###################
with open(__target_file__) as file_in: # Open source file for reading.
file_out = open(__target_file__+"_replaced", 'w') # Open destination file for writing.
for line in file_in: # Loop through source, line by line.
new_line = line.replace(__find__,__replace__) # Perform find & replace on current line.
print >>file_out, new_line # Write new line to destination file.
print "Done!"
print "End of script! Did it work?"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment