Newbie asks, how to get rid of the last four characters of a string?

Gerhard Häring gerhard.haering at opus-gmbh.net
Wed Jan 22 15:43:18 CET 2003


Christopher Culver <kricxjo.neniuspamajxo at yahoo.com> wrote:
> I'm somewhat new to Python and have run into this problem: I have a script
> whose first argument takes a MS Word file, which would end with .doc. I
> want to remove ".doc" and have just the file name without extension. Now,
> I know I can do this with regular expressions, but is there a function
> that can be used to chomp off the last four characters without having to
> import the re module?

#v+
>>> s = "foobar.doc"
>>> s[:-3]
'foobar.'
>>> s[:-4]
'foobar'
>>>
#v-

It's called 'slicing' in Python.

But this approach sucks. Better use the nice functions from os.path:

#v+
>>> import os.path
>>> os.path.splitext(s)
('foobar', '.doc')
>>> os.path.splitext(s)[0]
'foobar'
>>>
#v-

There's another possibility that sucks and that I can't recommend:

#v+
>>> s.split(".")[0]
'foobar'
>>> "my.word.document.doc".split(".")[0]
'my'
#v-

Just to add to your antipatterns list ;-)

os.path is the way to go.

Gerhard
-- 
Gerhard Häring
OPUS GmbH München
Tel.: +49 89 - 889 49 7 - 32
http://www.opus-gmbh.net/




More information about the Python-list mailing list