0

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?

5
  • I haven't checked to see if this solves your problem, but it may be useful to close your files at the end of the function. (or use with). Commented Nov 17, 2014 at 16:30
  • Thanks, it didn't solve the issue, but I forgot the closes in this rewrite. I've been playing with it and written it a few times :) Commented Nov 17, 2014 at 16:32
  • You need to apply each filter to each line in the file. So you got this inside out. Commented Nov 17, 2014 at 16:43
  • I thought that's what I was doing with "for line in file". I assumed it would iterate through the file for as long as the function was called with different parameters. Commented Nov 17, 2014 at 16:47
  • I'm debating just sticking everything in a list and iterate through that instead. Commented Nov 17, 2014 at 16:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.