Dave Melia
Dave Melia

Reputation: 317

Calling The Same Function Repeatedly In Python

I've written a little function with a replace() to clean up redirect files to strip out the logic and essentially leave in the pure English.

So far I've got:

def redir_cleanup(searchFor, replaceWith):
    inputFile = open('input', 'r')
    outputFile = open('output', 'w+')   

    for line in inputFile:
        print "Replacing %s with '%s'" % (searchFor, replaceWith)
        outputFile.write(line.replace(searchFor, replaceWith))

redir_cleanup("RedirectMatch permanent ", "")
redir_cleanup("RewriteRule ^", "")
redir_cleanup(" [L,R=301]", "")
redir_cleanup("RewriteCond %{QUERY_STRING} ^search\=(", "")
redir_cleanup(")$ [NC]", "")
redir_cleanup("\+", " ")
redir_cleanup("[NC,OR]", "")
redir_cleanup("RewriteRule ^.* ", "")

But it only strips the top call, do I need to loop them somehow?

Upvotes: 0

Views: 100

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

Your function reads from a file with one name, and then writes to a file with a different name. But the names never change, so it keeps using the same input. Try opening the files outside of the function and passing them in.

Upvotes: 1

Related Questions