But this is posixpath, right ? So '//x' like path will not occur as far as I guess ?<br><br><div class="gmail_quote">On Sat, Feb 20, 2010 at 8:35 AM, MRAB <span dir="ltr"><<a href="mailto:python@mrabarnett.plus.com">python@mrabarnett.plus.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div></div><div class="h5">Shashwat Anand wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
In the following code sample :<br>
<br>
def dirname(p):<br>
<br>
"""Returns the directory component of a pathname"""<br>
i = p.rfind('/') + 1<br>
<br>
head = p[:i]<br>
if head and head != '/'*len(head):<br>
<br>
head = head.rstrip('/')<br>
<br>
return head<br>
<br>
def dirname1(p):<br>
i = p.rfind('/') + 1<br>
<br>
head = p[:i]<br>
if head != '/':<br>
<br>
return head.rstrip('/') return head<br>
<br>
if __name__ == "__main__":<br>
p1 = '/Users/l0nwlf/Desktop'<br>
<br>
p2 = './'<br>
p3 = '/'<br>
p4 = '.'<br>
<br>
print dirname(p1), dirname1(p1)<br>
<br>
print dirname(p2), dirname1(p2)<br>
<br>
print dirname(p3), dirname1(p3)<br>
<br>
print dirname(p4), dirname1(p4)<br>
<br>
OUTPUT:<br>
<br>
/Users/l0nwlf /Users/l0nwlf<br>
. .<br>
/ /<br>
<br>
dirname() is a function taken from /Lib/posixpath.py. However i did not quite understood the usage of "if head and head != '/'*len(head):" and replaced it with more obvious way in dirname1().<br>
<br>
Am I right to do so ? Is dirname1() more pythonic ? Did I missed any edge cases here ?<br>
<br>
</blockquote></div></div>
What if the path is '//x'? The current dirname would return '//',<br>
whereas dirname1 would return ''.<br><font color="#888888">
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>