[Python-ideas] Updated PEP 428 (pathlib)

random832 at fastmail.us random832 at fastmail.us
Wed Mar 6 20:15:57 CET 2013



On Wed, Mar 6, 2013, at 13:28, Andrew Barnert wrote:
> 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.

Well, whatever you do is competing with p[:-len('.gz')] (and in shell,
your co-worker could have done ${z%.zip} in ksh/bash/POSIX) - unless you
have an example of an OS where the 'extension' component doesn't simply
append at the end (well, I suppose they're not case-sensitive on windows
- and how much weird long/short filename stuff does pathlib do on
windows?), I'm not sure how useful a function for this is.

Although, I was actually half-tempted to suggest p.basename -
p.extension and propose subtraction as a general "remove matching
suffix" operation on strings (or even without that, it could be defined
on a class that .basename returns)

I'm not sure what the use case is for doing this without already knowing
the extension, to be honest. This isn't an operation the basename(1)
tool supports, either [well, you could do $(basename $x .${x##*.}), but
it doesn't support it by itself]

-- 
Random832



More information about the Python-ideas mailing list