[Python-ideas] Updated PEP 428 (pathlib)

Andrew Barnert abarnert at yahoo.com
Wed Mar 6 19:28:59 CET 2013

On Mar 6, 2013, at 7:45, random832 at fastmail.us wrote:

> On Tue, Mar 5, 2013, at 15:29, Andrew Barnert wrote:
>> Correct me if I'm remembering wrong, but the posix basename(1) tool can
>> strip both dirnames _and_ extensions.
>> So, any confusion here has a solid precedent.
> Yes, but it requires you to pass in the extension.
> So what about
> p = '/foo/pathlib.tar.gz'
> p.basename() == 'pathlib.tar.gz'
> p.basename('.gz') == 'pathlib.tar'
> p.basename('.tar.gz') == 'pathlib'
> p.basename('.a') exception? 'pathlib.tar.gz'?
> p.basename(True) == 'pathlib.tar'
> p.basename(1) == 'pathlib.tar'
> p.basename(2) == 'pathlib' ?
I was just pointing out that the name already does double duty, and therefore it's a few decades too late for people to complain about confusion, not suggesting that we emulate it.

But now that I see your examples, it's not a bad idea. Anyone familiar with unix will immediately understand what basename('.gz') is doing and appreciate the shortcut, and the alternate forms you provided aren't surprising.

The problem is that I think people will still want a stripext/stem/root/whatever function that removes the extension without also removing the dirname. In fact, I've got a script here where one of my coworkers has written $(dirname $z)/$(basename $z .zip) to get around it.

And I don't think p.basename(True, False) is a viable answer. Besides, what names would you give the two flags? If there were good names for those, there would be good names for the two separate functions, right?

Personally, I don't have a problem with the stem or stripext suggestions (although to me, the former implies stripping all extensions, the latter just one), but I guess they haven't gotten much traction.

More information about the Python-ideas mailing list