[Python-ideas] Small enhancement to os.path.splitext

Bruce Leban bruce at leapyear.org
Tue Apr 20 17:48:25 CEST 2010


When would you know in advance you want 2 parts? Compare file.tar.gz to
backup.2010.04.20.tar. I'd think the usual case would be

split
if gz or zip or...:
  split again

--- Bruce
(via android)

On Apr 20, 2010 8:36 AM, "Tarek Ziadé" <ziade.tarek at gmail.com> wrote:

Hello

Currently, os.path.splitext will split a string giving you the piece
behind the last dot:

>>> os.path.splitext('file.tar.gz')
('file.tar', '.gz')

In some cases, what we really want is the two last parts when
splitting on the dots (like in my example).

What about providing an extra argument to be able to grab more than one dot
?

>>> os.path.splitext('file.tar.gz', numext=2)
('file', '.tar.gz')

If numext > numbers of dots, it will just split after the first dot:

>>> os.path.splitext('file.tar', numext=2)
('file', '.tar')


What do you think ?

Regards
Tarek






--
Tarek Ziadé | http://ziade.org
_______________________________________________
Python-ideas mailing list
Python-ideas at python.org
http://mail.python.org/mailman/listinfo/python-ideas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100420/b590ad73/attachment.html>


More information about the Python-ideas mailing list