<?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>little brain &#187; python</title>
	<atom:link href="http://littlebrain.org/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://littlebrain.org</link>
	<description>me and my brain</description>
	<lastBuildDate>Sun, 20 Jun 2010 15:31:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Replace Space With Underscore in Filename</title>
		<link>http://littlebrain.org/2008/10/19/replace-space-with-undescore-in-filename/</link>
		<comments>http://littlebrain.org/2008/10/19/replace-space-with-undescore-in-filename/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 13:37:12 +0000</pubDate>
		<dc:creator>Aldiantoro Nugroho</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rename]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://littlebrain.org/?p=195</guid>
		<description><![CDATA[This is merely my personal note. I was about to rename all my files under a directory. I need to replace the space with underscore character ('_'). My first thought was a simple bash script to do that. Apparently, it&#8217;s &#8230; <a href="http://littlebrain.org/2008/10/19/replace-space-with-undescore-in-filename/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is merely my personal note. I was about to rename all my files under a directory. I need to replace the space with underscore character <code>('_')</code>. My first thought was a simple bash script to do that. Apparently, it&#8217;s been very long time since my last bash coding session. I spent 15 minutes reading how to read all files and rename them. And I got nothing.</p>
<p>Luckily, I know python. Stupid me. Why didn&#8217;t I think it at first time. It was couple minutes of python and all the spaces were replaced by underscores. Thanks to python. All I did were</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="text" style="font-family:monospace;color: #ccc; font: 12px Consolas, Lucida Console, Monaco, monospace;">$ python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
[GCC 4.3.2] on linux2
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import os
&gt;&gt;&gt; files = os.listdir('./')
&gt;&gt;&gt; for f in files:
...     os.rename(f, f.replace(' ', '_'))
... 
&gt;&gt;&gt;</pre></td></tr></table></div>

<p>Or if you want to save in a script, you could you this</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;color: #ccc; font: 12px Consolas, Lucida Console, Monaco, monospace;"><span class="co1">#!/usr/bin/python</span>
&nbsp;
<span class="kw1">import</span> <span class="kw3">os</span>
<span class="kw1">import</span> <span class="kw3">sys</span>
&nbsp;
files = <span class="kw3">os</span>.<span class="me1">listdir</span><span class="br0">&#40;</span><span class="kw3">sys</span>.<span class="me1">argv</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span>
<span class="kw1">for</span> f <span class="kw1">in</span> files:
    <span class="kw3">os</span>.<span class="me1">rename</span><span class="br0">&#40;</span>f, f.<span class="me1">replace</span><span class="br0">&#40;</span><span class="st0">' '</span>, <span class="st0">'_'</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre></td></tr></table></div>

<p>The script takes the directory path as the argument. You could modify the script to use regex to have a better rename rule <img src='http://littlebrain.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://littlebrain.org/2008/10/19/replace-space-with-undescore-in-filename/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
