<div class="highlight"><pre><font style="font-family: tahoma,sans-serif;" size="2"><span class="k">In the following code sample :<br><br><br>def</span> <span class="nf">dirname</span><span class="p">(</span><span class="n">p</span><span class="p">):</span><br>

    <span class="sd">"""Returns the directory component of a pathname"""</span><br>    <span class="n">i</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span> <span class="o">+</span> <span class="mf">1</span><br>

    <span class="n">head</span> <span class="o">=</span> <span class="n">p</span><span class="p">[:</span><span class="n">i</span><span class="p">]</span><br>    <span class="k">if</span> <span class="n">head</span> <span class="ow">and</span> <span class="n">head</span> <span class="o">!=</span> <span class="s">'/'</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">head</span><span class="p">):</span><br>

        <span class="n">head</span> <span class="o">=</span> <span class="n">head</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span><br>

    <span class="k">return</span> <span class="n">head</span><br><br><span class="k">def</span> <span class="nf">dirname1</span><span class="p">(</span><span class="n">p</span><span class="p">):</span><br>   <span class="n">i</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span> <span class="o">+</span> <span class="mf">1</span><br>

   <span class="n">head</span> <span class="o">=</span> <span class="n">p</span><span class="p">[:</span><span class="n">i</span><span class="p">]</span><br>   <span class="k">if</span> <span class="n">head</span> <span class="o">!=</span> <span class="s">'/'</span><span class="p">:</span><br>

        <span class="k">return</span> <span class="n">head</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span>    <br>   <span class="k">return</span> <span class="n">head</span><br>

   <br>   <br><br><span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span><br>   <span class="n">p1</span> <span class="o">=</span> <span class="s">'/Users/l0nwlf/Desktop'</span><br>

   <span class="n">p2</span> <span class="o">=</span> <span class="s">'./'</span><br>   <span class="n">p3</span> <span class="o">=</span> <span class="s">'/'</span><br>   <span class="n">p4</span> <span class="o">=</span> <span class="s">'.'</span><br>

<br>   <span class="k">print</span> <span class="n">dirname</span><span class="p">(</span><span class="n">p1</span><span class="p">),</span> <span class="n">dirname1</span><span class="p">(</span><span class="n">p1</span><span class="p">)</span><br>

   <span class="k">print</span> <span class="n">dirname</span><span class="p">(</span><span class="n">p2</span><span class="p">),</span> <span class="n">dirname1</span><span class="p">(</span><span class="n">p2</span><span class="p">)</span><br>

   <span class="k">print</span> <span class="n">dirname</span><span class="p">(</span><span class="n">p3</span><span class="p">),</span> <span class="n">dirname1</span><span class="p">(</span><span class="n">p3</span><span class="p">)</span><br>

   <span class="k">print</span> <span class="n">dirname</span><span class="p">(</span><span class="n">p4</span><span class="p">),</span> <span class="n">dirname1</span><span class="p">(</span><span class="n">p4</span><span class="p">)</span></font><br>

<br><br><font style="font-family: arial,helvetica,sans-serif;" size="2">OUTPUT:<br><br>/Users/l0nwlf /Users/l0nwlf<br>. .<br>/ /<br><br><br>dirname() is a function taken from /Lib/posixpath.py. However i did not quite understood the usage of "</font><font><font style="font-family: tahoma,sans-serif;" size="2"><span class="k">if</span> <span class="n">head</span> <span class="ow">and</span> <span class="n">head</span> <span class="o">!=</span> <span class="s">'/'</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">head</span><span class="p">):"</span></font></font><font style="font-family: arial,helvetica,sans-serif;" size="2"> and replaced it with more obvious way in dirname1().<br>

Am I right to do so ? Is dirname1() more pythonic ? Did I missed any edge cases here ?<br><br>Regards,<br>~l0nwlf<br></font><br></pre></div>