removing extension

Arnaud Delobelle arnodel at googlemail.com
Sun Apr 27 13:00:54 EDT 2008


Marc 'BlackJack' Rintsch <bj_666 at gmx.net> writes:

> Not exactly.  In the case of no extension `os.path.splitext()` still works:
>
> In [14]: 'foo/bar.txt'.rsplit('.')
> Out[14]: ['foo/bar', 'txt']
>
> In [15]: 'foo/bar'.rsplit('.')
> Out[15]: ['foo/bar']
>
> In [16]: os.path.splitext('foo/bar')
> Out[16]: ('foo/bar', '')

And crucially, it still works correctly if the file has no
extension but a directory in the path has one:

>>> 'foo.baz/bar'.rsplit('.')
['foo', 'baz/bar']
>>> os.path.splitext('foo.baz/bar')
('foo.baz/bar', '')

Conclusion: forget about str.rsplit() (which I suggested), use
os.path.splitext() instead :)

-- 
Arnaud



More information about the Python-list mailing list