<?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>soledad penadés &#187; files</title>
	<atom:link href="http://soledadpenades.com/tag/files/feed/" rel="self" type="application/rss+xml" />
	<link>http://soledadpenades.com</link>
	<description>repeat 4[fd 100 rt 90]</description>
	<lastBuildDate>Sun, 29 Jan 2012 23:03:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Too many open files</title>
		<link>http://soledadpenades.com/2009/11/10/too-many-open-files/</link>
		<comments>http://soledadpenades.com/2009/11/10/too-many-open-files/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 12:44:50 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[lsof]]></category>
		<category><![CDATA[trick]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=1655</guid>
		<description><![CDATA[I am doing something that requires manipulating a lot of files, and I fell in the classical too many open files error trap. A way of finding out which files are being used by a process is to type ps -ax in a terminal, then identify the guilty process and its PID. Let&#8217;s imagine its [...]]]></description>
			<content:encoded><![CDATA[<p>I am doing something that requires manipulating a lot of files, and I fell in the classical <em>too many open files</em> error trap.</p>
<p>A way of finding out which files are being used by a process is to type</p>
<div class="syhi_block"><code><span style="color: #c20cb9; font-weight: bold;">ps</span> <span style="color: #660033;">-ax</span></code></div>
<p>in a terminal, then identify the guilty process and its PID. Let&#8217;s imagine its PID is 9090. Now to list every one of its open files you just run this (again, in the terminal):</p>
<div class="syhi_block"><code>lsof <span style="color: #660033;">-p</span> <span style="color: #000000;">9090</span></code></div>
<p>Or you can get a raw estimate by piping that through wc and getting the number of lines in the return value of lsof:</p>
<div class="syhi_block"><code>lsof <span style="color: #660033;">-p</span> <span style="color: #000000;">9090</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">wc</span> <span style="color: #660033;">-l</span></code></div>
<p>That will return a number, like for example &#8220;33&#8243;.</p>
<p>It&#8217;s interesting to know that the output from lsof does not only show files in the windows way of referring to a file as a folder or data written in the disk, but returns files in the UNIX way, i.e., <q>everything is a file</q>, including pipes, sockets, files-files, etc.</p>
<p>I have changed my code meanwhile to be a bit more austere in regards to the number of open files, but this is an interesting tool nevertheless. If you run it for example with Firefox, you can even see which font files Firefox is using:</p>
<div class="syhi_block"><code>...<br />
firefox 3847 sole &nbsp;mem &nbsp; &nbsp;REG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8,1 &nbsp; 224692 &nbsp; 272248 /usr/share/fonts/truetype/msttcorefonts/Arial_Bold_Italic.ttf<br />
firefox 3847 sole &nbsp;mem &nbsp; &nbsp;REG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8,1 &nbsp; 622020 &nbsp; &nbsp; 6209 /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf<br />
...</code></div>
<p>Or which plug-ins has it loaded:</p>
<div class="syhi_block"><code>...<br />
firefox 3847 sole &nbsp;mem &nbsp; &nbsp;REG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8,1 &nbsp; 101536 &nbsp; &nbsp;13774 /usr/lib/mozilla/plugins/libtotem-cone-plugin.so<br />
firefox 3847 sole &nbsp;mem &nbsp; &nbsp;REG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8,1 &nbsp; 117960 &nbsp; &nbsp; &nbsp;741 /var/lib/flashplugin-installer/npwrapper.libflashplayer.so<br />
...</code></div>
<p>etc etc :)</p>
]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2009/11/10/too-many-open-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Split files into folders by letter</title>
		<link>http://soledadpenades.com/2008/05/13/split-files-into-folder-by-letter/</link>
		<comments>http://soledadpenades.com/2008/05/13/split-files-into-folder-by-letter/#comments</comments>
		<pubDate>Tue, 13 May 2008 10:24:42 +0000</pubDate>
		<dc:creator>sole</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[folders]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[trick]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://soledadpenades.com/?p=699</guid>
		<description><![CDATA[I had a lot of files in one folder. It is not very practical to browse the folder that way, so I decided to create a little script which would split the files into different folders, using the first letter of the file for naming the folder, as in a, b, c, d&#8230; but using [...]]]></description>
			<content:encoded><![CDATA[<p>I had a lot of files in one folder. It is not very practical to browse the folder that way, so I decided to create a little script which would split the files into different folders, using the first letter of the file for naming the folder, as in a, b, c, d&#8230; but <strong>using Python</strong> this time! </p>
<div class="syhi_block"><code><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">shutil</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; folder = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
<span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; folder = <span style="color: #483d8b;">'.'</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">listdir</span><span style="color: black;">&#40;</span>folder<span style="color: black;">&#41;</span>:<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; full_path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>folder, item<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>full_path<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">continue</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; dst_folder = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>folder, item<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>dst_folder<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">mkdir</span><span style="color: black;">&#40;</span>dst_folder<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">shutil</span>.<span style="color: black;">move</span><span style="color: black;">&#40;</span>full_path, <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>dst_folder, item<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></code></div>
<p><a href="http://github.com/sole/snippets/blob/master/utils/filesystem_manipulation/split_files_into_folders_by_letter.py">github</a> (this should always be the most up to date version)</p>
<p>To use simply go to your favourite terminal window and type</p>
<div class="syhi_block"><code>python split_files_into_folders_by_letter.py <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>messy<span style="color: #000000; font-weight: bold;">/</span>folder</code></div>
<p>It will pick the first letter of each file, create a folder with the lowercased letter (if it doesn&#8217;t exist yet) and move the file to the folder. It won&#8217;t move folders! (because of the os.path.isdir check).</p>
<p>If there&#8217;s any <em>pythonista</em> in the audience with ideas for improving this, feel free to leave your suggestions in the comments, thanks! :-)</p>
<h3>Note</h3>
<p>There wasn&#8217;t any special reason for not doing it with Ruby; I simply wanted to know what the differences would be. And while the semantics aren&#8217;t very different (e.g. where it says <em>os.path.join</em> we would use <em>File.join</em> in Ruby), there is a huge difference between them and it&#8217;s called <strong>documentation</strong>. Python documentation is hosted in Python&#8217;s website, it is reviewed (and mostly written too) by Guido van Rossum (Python&#8217;s author) and it&#8217;s up to date and very easy to browse and read. Whereas Ruby docs were hosted elsewhere until very recently, are a pain to browse and pretty much a dump of automatically generated docs from source, which I find very uncomfortable to use. It still feels a little bit weird to use python&#8217;s indenting style but I&#8217;m really liking it. A pity there isn&#8217;t a Hpricot in python :-)</p>
 <p><a href="http://soledadpenades.com/?flattrss_redirect&amp;id=699&amp;md5=18d594b4795ece9995afb3d422f28dc7" title="Flattr" target="_blank"><img src="http://soledadpenades.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://soledadpenades.com/2008/05/13/split-files-into-folder-by-letter/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

