Popular recipes tagged "paths" but not "cli"http://code.activestate.com/recipes/tags/paths-cli/2010-06-16T23:17:20-07:00ActiveState Code Recipesdealing with directory paths with ~ (Python) 2010-06-16T23:17:20-07:00roopeshvhttp://code.activestate.com/recipes/users/4174204/http://code.activestate.com/recipes/577270-dealing-with-directory-paths-with/ <p style="color: grey"> Python recipe 577270 by <a href="/recipes/users/4174204/">roopeshv</a> (<a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/expanding/">expanding</a>, <a href="/recipes/tags/paths/">paths</a>). Revision 2. </p> <p>Dealing with directory paths which start with <code>~</code> which are passed as paramaters, to <code>os</code> module functions.</p> <p>Here is what I think python doesn't do for me:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; import os # suppose my home = curdir = /home/rv &gt;&gt;&gt; os.path.abspath('.') '/home/rv' </code></pre> <p>Now if I want to go to folder <code>/home/rv/test</code> if there is no folder by name /home/rv/~/test/</p> <pre class="prettyprint"><code># This is what happens by default. &gt;&gt;&gt; os.path.abspath('~/test') '/home/rv/~/test' &gt;&gt;&gt; os.chdir('/home/rv/some/dir') # doesn't matter if the resulting path exists or not. &gt;&gt;&gt; os.path.abspath('~/test') 'home/rv/some/dir/~/test' </code></pre> <p>This would be more sensible I guess:</p> <pre class="prettyprint"><code># if /home/rv/~/test doesn't exist &gt;&gt;&gt; os.path.abspath('~/test') '/home/rv/test' </code></pre> Normalizing paths (Python) 2010-01-26T14:39:00-08:00Gustavo Nareahttp://code.activestate.com/recipes/users/4172869/http://code.activestate.com/recipes/577023-normalizing-paths/ <p style="color: grey"> Python recipe 577023 by <a href="/recipes/users/4172869/">Gustavo Narea</a> (<a href="/recipes/tags/absolute_path/">absolute_path</a>, <a href="/recipes/tags/normalization/">normalization</a>, <a href="/recipes/tags/normalize/">normalize</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/paths/">paths</a>). Revision 3. </p> <p>While dealing with paths, it's often necessary to make sure they all have the same structure so any operation you perform on them can be reliable, specially when it comes to comparing two or more paths. Unusual paths like "/this//is//a///path" or "another/path" can cause unexpected behavior in your application and this is where this function comes into play.</p>