Find and Replace in files of a folder including subfolders
<p>I'm coding a script to replace (Ex: old ip address) in all configuration files in a folder including subfolders.<br>
Here's my script, it's not yet tested, I need your help and improvement, thanks. (newbie to Python) </p>

<pre><code>#!/usr/bin/env python
'''
my-find-replace-sth.py [/etc] [text_to_search] [text_to_replace]

Find all text files only in "/etc" and replace Old partern with New partern as
arguments input from user.

'''

import os
import sys

def FindReplaceInFiles(dirToProcess, textToSearchFor, textToReplaceWith):
    for rootdir, subdir, files in os.walk(dirToProcess):
        for file in files:          

    file = fileToProcess
    oldFileName  = 'old_' + fileToProcess
    tempFileName = 'temp_' + fileToProcess

    # If there's already an 'old_' prefixed backup file there from
    # a previous run, remove it...
    if file == oldFileName:
            os.remove( oldFileName )

    tempFile = open( tempFileName, 'w' )
        for line in tempFile:
        if textToSearch in line:
            line.replace(textToSearchFor,textToReplaceWith)
    tempFile.close()         

def main():
    dirToProcess = '/etc'
    textToSearchFor   = sys.argv[1]  # Old partern 
    textToReplaceWith = sys.argv[2]  # New partern

    FindReplaceInFiles()

if __name__ == '__main__':
usage = "Usage: %s ['/etc'] [text_to_search] [text_to_replace]" % os.path.basename(sys.argv[0])

if len( sys.argv ) &lt; 2:
    print usage

else: 
    print "There are %s args " %len( sys.argv )
    print "Please specify the [text_to_search] and [texte_to_replace]. Thanks."

    main()      
</code></pre>

<p><strong>Can someone improve this code for better ?</strong> _<em>or other way to code it ?</em>_</p>
