basically I infer that : dirname = path - basename, like for path = '//x', basename = x, hence dirname = '//'<br><br><div class="gmail_quote">On Sat, Feb 20, 2010 at 8:47 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 class="im">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;">
But this is posixpath, right ? So '//x' like path will not occur as far as I guess ?<br>
<br>
</blockquote></div>
Can you guarantee that? It's safer to just leave it as it is, just in<br>
case! :-)<div><div></div><div class="h5"><br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
On Sat, Feb 20, 2010 at 8:35 AM, MRAB <<a href="mailto:python@mrabarnett.plus.com" target="_blank">python@mrabarnett.plus.com</a> <mailto:<a href="mailto:python@mrabarnett.plus.com" target="_blank">python@mrabarnett.plus.com</a>>> wrote:<br>
<br>
Shashwat Anand wrote:<br>
<br>
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<br>
did not quite understood the usage of "if head and head !=<br>
'/'*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<br>
any edge cases here ?<br>
<br>
What if the path is '//x'? The current dirname would return '//',<br>
whereas dirname1 would return ''.<br>
<br>
</blockquote>
<br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>