<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>And Slaters Go Plop &#187; files</title>
	<atom:link href="http://damian.peterson.net.nz/tag/files/feed/" rel="self" type="application/rss+xml" />
	<link>http://damian.peterson.net.nz</link>
	<description>The Bloggery of Damian Peterson</description>
	<lastBuildDate>Thu, 03 Nov 2011 02:39:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Search and Replace text in files with Python</title>
		<link>http://damian.peterson.net.nz/2007/11/29/search-and-replace-text-in-files-with-python/</link>
		<comments>http://damian.peterson.net.nz/2007/11/29/search-and-replace-text-in-files-with-python/#comments</comments>
		<pubDate>Thu, 29 Nov 2007 02:10:03 +0000</pubDate>
		<dc:creator>Damian</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[subdirectories]]></category>
		<category><![CDATA[text]]></category>

		<guid isPermaLink="false">http://damian.peterson.net.nz/2007/11/29/search-and-replace-text-in-files-with-python/</guid>
		<description><![CDATA[#! /usr/bin/python2.5 import os mydir = "/path/to/directory" mysearch = "text to find" myreplace = "Text to replace" def doReplace(filePath): fin = open(filePath, "r") s = fin.read() fin.flush() fin.close() fout = open(filePath, "w") s = s.replace(mysearch, myreplace) fout.write(s) fout.close() for root, dirs, files in os.walk(mydir): for f in files: name, ext = os.path.splitext(f) if ext == [...]]]></description>
			<content:encoded><![CDATA[<p><code> </code></p>
<pre>#!  /usr/bin/python2.5
import os

mydir = "/path/to/directory"
mysearch = "text to find"
myreplace = "Text to replace"

def doReplace(filePath):
    fin = open(filePath, "r")
    s = fin.read()
    fin.flush()
    fin.close()
    fout = open(filePath, "w")
    s = s.replace(mysearch, myreplace)
    fout.write(s)
    fout.close()

for root, dirs, files in os.walk(mydir):
    for f in files:
        name, ext = os.path.splitext(f)
        if ext == '.html':
            doReplace(root + '/' + f)</pre>
<p>Explanation: This will find all files ending in .html in the directory specified in mydir along with all matching files in any subfolders and will replace the text specified in mysearch with the text in myreplace. It&#8217;s only been tested on Linux but with a bit of tweaking will run on Windows and Mac.</p>
]]></content:encoded>
			<wfw:commentRss>http://damian.peterson.net.nz/2007/11/29/search-and-replace-text-in-files-with-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

